Comments (2)
Before I tell you what's wrong, let me tell you that what you are trying to do, doesn't make much sense. Waiting within a request on a published message will block the request. It can be done, but it is a bad idea.
To focus on the problem: you are returning a response from the subscription callback, which is discarded (the MqttClient
expects no return value from the closure). You also have not called $mqtt->loop()
, which is required for a subscription. A working example would be:
$result = [];
$mqtt = MQTT::connection();
$mqtt->subscribe('stat/xxxxxxxx/POWER', function (string $topic, string $message) use ($mqtt, &$result) {
$result['topic'] = $topic;
$result['message'] = $message;
$mqtt->interrupt();
}, 0);
$mqtt->loop(true);
return response()->json($result, 200);
But I repeat myself: this makes no sense. You should not be waiting for published messages in a request. Most php-fpm
deployments are configured with a fixed number of workers, which means the whole application will already be unreachable after 10 concurrent requests or so. If you need to process incoming data, use a console command for it.
from laravel-client.
Before I tell you what's wrong, let me tell you that what you are trying to do, doesn't make much sense. Waiting within a request on a published message will block the request. It can be done, but it is a bad idea.
To focus on the problem: you are returning a response from the subscription callback, which is discarded (the
MqttClient
expects no return value from the closure). You also have not called$mqtt->loop()
, which is required for a subscription. A working example would be:$result = []; $mqtt = MQTT::connection(); $mqtt->subscribe('stat/xxxxxxxx/POWER', function (string $topic, string $message) use ($mqtt, &$result) { $result['topic'] = $topic; $result['message'] = $message; $mqtt->interrupt(); }, 0); $mqtt->loop(true); return response()->json($result, 200);
But I repeat myself: this makes no sense. You should not be waiting for published messages in a request. Most
php-fpm
deployments are configured with a fixed number of workers, which means the whole application will already be unreachable after 10 concurrent requests or so. If you need to process incoming data, use a console command for it.
Working, Thank you for your advice.
from laravel-client.
Related Issues (20)
- Can not transfer data bigger than 1MB HOT 7
- could be use/configured as broadcast driver? HOT 3
- Godaddy Server(Shared Hosting) connecting to AWS iot core showing error. HOT 1
- Which Version of Laravel does it support? HOT 1
- Can this be used to make a MQTT sink? HOT 1
- Does it support auto reconnect? HOT 1
- Websocket port HOT 3
- Can the client publish messages on two different ports (TCP and WebSockets)? HOT 1
- Publish message as a bytes array HOT 8
- Support for Laravel 10 HOT 2
- Detect if response is not received from MQTT broker for specified duration. HOT 8
- SSL/TLS connection configuration HOT 6
- Is it possible to do subscribing to mutliple topics on 1 command? HOT 1
- How do you reset the command under supervisor when something changes in controller? HOT 3
- The broker did not acknowledge the connection attempt within the configured connection timeout period HOT 4
- plans to support php-mqtt client v2.0? HOT 2
- Cannot dispatch job inside subscribe closure HOT 1
- Certificate file on different disk HOT 1
- Settings without useBlockingSocket option HOT 1
- Enhancement Proposal: Addition of interrupt and subscribe methods to MQTT Facade and ConnectionManager Class HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-client.