GithubHelp home page GithubHelp logo

subscribe not working about laravel-client HOT 2 CLOSED

php-mqtt avatar php-mqtt commented on August 16, 2024
subscribe not working

from laravel-client.

Comments (2)

Namoshek avatar Namoshek commented on August 16, 2024 1

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.

pheelaphong avatar pheelaphong commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.