-
Notifications
You must be signed in to change notification settings - Fork 8
/
subscribe.php
53 lines (41 loc) · 1.31 KB
/
subscribe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
use \Mosquitto\Client as MosquittoClient;
/**
* Created by PhpStorm.
* Company Anka Technologies
* User: TNChalise <[email protected]>
* Time: 1:00 PM
*/
$client = new MosquittoClient();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("192.168.0.130", 1883, 5); //Change Accordingly. TCP/IP connection is supported by mqtt.
//Please refer to topic/channel semantics [http://mosquitto.org/man/mqtt-7.html]
$client->subscribe('mqtt://my-topic/messages/#', 1);
while (true) {
$client->loop();
$mid = $client->publish('mqtt://my-topic/messages/userId', "Hello from PHP at " . date('Y-m-d H:i:s'), 1, 0);
echo "Sent message ID: {$mid}\n";
$client->loop();
sleep(2);
}
$client->disconnect();
unset($client);
function connect($r)
{
echo "I got code {$r}\n";
}
function subscribe()
{
echo "Subscribed to a topic\n";
}
function message($message)
{
printf("Got a message ID %d on topic %s with payload:\n%s\n\n", $message->mid, $message->topic, $message->payload);
}
function disconnect()
{
echo "Disconnected cleanly\n";
}