-
Notifications
You must be signed in to change notification settings - Fork 1
/
paho_mqtt_sub.rb
37 lines (29 loc) · 888 Bytes
/
paho_mqtt_sub.rb
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
require 'paho-mqtt'
### Create a simple client with default attributes
client = PahoMqtt::Client.new
### Register a callback on message event to display messages
message_counter = 0
client.on_message do |message|
puts "Message recieved on topic: #{message.topic}\n>>> #{message.payload}"
message_counter += 1
end
### Register a callback on suback to assert the subcription
waiting_suback = true
client.on_suback do
waiting_suback = false
puts "Subscribed"
end
### Connect to the eclipse test server on port 1883 (Unencrypted mode)
client.connect('localhost', 1883)
### Subscribe to a topic
client.subscribe(['/test/', 2])
### Waiting for the suback answer and excute the previously set on_suback callback
while waiting_suback do
sleep 0.001
end
# Listen forever
while 1 do
sleep 0.001
end
### Calling an explicit disconnect (Should never get here...)
client.disconnect