Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscribe not working #4

Closed
pheelaphong opened this issue Jan 13, 2021 · 2 comments
Closed

subscribe not working #4

pheelaphong opened this issue Jan 13, 2021 · 2 comments

Comments

@pheelaphong
Copy link


$mqtt = MQTT::connection();
$mqtt->subscribe('stat/xxxxxxxx/POWER', function (string $topic, string $message) {
        return response()->json(['topic' => $topic, 'message' => $message], 200);
}, 0);

return empty


But on MQTT Dashboard (App android) this topic working
And publish working

@Namoshek
Copy link
Collaborator

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.

@pheelaphong
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants