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

Need to publish based on incoming message #92

Open
rts-ssoundar619 opened this issue Jul 26, 2018 · 1 comment
Open

Need to publish based on incoming message #92

rts-ssoundar619 opened this issue Jul 26, 2018 · 1 comment

Comments

@rts-ssoundar619
Copy link

rts-ssoundar619 commented Jul 26, 2018

I have tried to publish a message from the message() function and I am getting this error.

function message($message) {
$client->publish('MCON', 'jkhajkdfadfm,jka jkhkadf jklhadf ', 0, false);
    //topic payload
    echo '\n';
    print_r($message->topic);
    echo '\n';
    $topic = explode('/', $message->topic);
    if (sizeof($topic) > 2) {
        $topic[2]($message->payload);
    } else {
        $topic[0]($message->payload);
    }
}

and I am getting the following error. Please help me to sort out the requirement.

Subscribed to a topic
PHP Notice:  Undefined variable: client in /var/www/html/test.php on line 35
PHP Fatal error:  Uncaught Error: Call to a member function publish() on null in /var/www/html/test.php:35
Stack trace:
#0 [internal function]: message(Object(Mosquitto\Message))
#1 /var/www/html/test.php(17): Mosquitto\Client->loopForever()
#2 {main}

thrown in /var/www/html/test.php on line 35
Thanks.

@1biot
Copy link

1biot commented Mar 27, 2020

try this:

$client->onMessage(
    function ($message) using ($client) {
        $client->publish('MCON', 'jkhajkdfadfm,jka jkhkadf jklhadf ', 0, false);
        //topic payload
        echo '\n';
        print_r($message->topic);
        echo '\n';
        $topic = explode('/', $message->topic);
        if (sizeof($topic) > 2) {
            $topic[2]($message->payload);
        } else {
            $topic[0]($message->payload);
        }
    }
});

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