Skip to content
Chris Petersen edited this page Oct 16, 2014 · 1 revision

mqtt-subscribe subscribes to topic from the MQTT broker.

Parameter Description
t MQTT broker
topic Topic name string
qos QOS level: 0 fire-and-forget, 1 send-at-least-once , 2 send-exactly-once

Example

Example 1: Make a connection to a mosquitto broker running on localhost:1883, subscribe to two topics, publish some data to them and wait for the results to show. Then unsubscribe from one topic and show that it's handler is no longer called. Finally close the connection properly.

> (define m (make-mqtt 'host "127.0.0.1" 'port 1883 
                        'handler (lambda (topic val) 
                          (for-each display (list topic ": " val "\n")))))
> (thread-sleep! 0.5)
> (mqtt-subscribe m "Test1" 2)
#t
> (mqtt-subscribe m "Test2" 2)
#t
> (mqtt-publish m "Test1" 123 2 0)
#t
> (mqtt-publish m "Test2" 123456 2 0)
#t
> (thread-sleep! 1.)
Test1: 123
Test2: 123456
> (mqtt-unsubscribe m "Test1")
#t
> (mqtt-publish m "Test1" "blah" 2 0)
#t
> (mqtt-publish m "Test2" "LN1" 2 0)
#t
> (thread-sleep! 1.)
Test2: LN1
> (mqtt-destroy m)
#t
Clone this wiki locally