0

I'm using Mosquitto/Client php library. I have made connected properly between broker and client. When i execute publisher and subscriber then subscriber displays a number of messages but I want to get a single published message when publisher publish the message. How will i get a single message by subscribing the published message?

Subscriber.php

define('BROKER', '192.168.0.13');
define('PORT', 1883);
define('CLIENT_ID', "pubclient_" + getmypid());
$client = new Mosquitto\Client(CLIENT_ID);
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect(BROKER, PORT, 60);
$client->subscribe('test/foo/bar', 1);
$client->loopForever();
function connect($r) {
    echo "Received response code {$r}\n";
}
function subscribe() {
    echo "Subscribed to a topic\n";
}
function message($message) {
    printf("Got a message on topic %s with payload:\n%s\n", $message->topic, $message->payload);
}
function disconnect() {
    echo "Disconnected cleanly\n";
}

publisher.php

define('BROKER', '192.168.0.13');
define('PORT', 1883);
define('CLIENT_ID', "pubclient_" + getmypid());
$client = new Mosquitto\Client(CLIENT_ID);
$client->connect(BROKER, PORT, 5);
while ($client->loop() == 0) {
    $message = "Test message at " . date("Y-m-d H:i:s");
    $client->publish('test/foo/bar', $message, 1, false);
    $client->loop();
    sleep(1);
}

I want to publish/subscribe in a specific interval. Publisher.php publishes a topic with a message in a interval time period and subscriber.php get that topic with that message in a interval time period.

Salman Quader
  • 195
  • 2
  • 13
  • 1
    The question is unclear: what time interval? You are publishing every second, and it sounds the subscriber receives a message every second. Does the subscriber have some other, longer interval in which you want the subscriber to accept only one message? The only sign of that in your code is `$client->connect(BROKER, PORT, 60);`, where it looks like you tried to specify a 1-minute interval in the place meant for the subscriber's client ID (https://www.cloudamqp.com/docs/php_mqtt.html). – MBer Apr 10 '19 at 17:33
  • i published a topic in every second and subscribed a topic in every second. But when i run subscriber.php then it provides a packet of a number of messages in every minute. I want to subscribe and get a singe message in a specific time period. I run your code but subscribe page doesn't get response when i set $mqtt->proc() == o then it get returns a single message then it disconnects from the broker. – Salman Quader Apr 11 '19 at 06:34
  • My posted code is working in ubuntu terminal properly. When i run the subscribe.php file and publish.php file in ubuntu individual two terminal then subscribe displays subscribing message in every second[if publish something then subscribe that message otherwise pingreq]. BUt when i browse this same file from web browser then subscribe page display a packet of many messages in every minutes – Salman Quader Apr 11 '19 at 10:39
  • "Subscribe" means to register with the broker to receive copies of all future messages (the client only subscribes once). So the problem is that messages are published once every second, but all arrive at the same time once per minute. It is possible that the client only asks the broker to provide the new messages once a minute. I don't know phpMQTT well enough to suggest a reason, but have you tried changing the 60 to a smaller number? – MBer Apr 11 '19 at 22:26

0 Answers0