Jörgen Brandt
The mqtt-client module is a Racket MQTT client implementation based on libpaho-mqtt3. It allows the user to set up an MQTT client, create a connection to MQTT message broker, subscribe to topics, and to send or receive messages. The module uses a C-based, dynamically linked library that needs to reside in the operating system.
Below, we list a simple example for the way, this module might be used. To send and receive messages we set up different contexts, two of which are mandatory: a client context and, nested in it, a connection context. Optionally, we can change some parameters using contexts. Here, for example, we change the quality of service (QOS) when publishing to qos-1.
First, we set up a client using mqtt/with-client . Herein, we provide the message broker’s URL, "localhost", and a client id, "client1", for our application.
Next, we set up a connection to an MQTT message broker using . All connection parameters have reasonable default values but, here, we set the keep-alive interval to 20 seconds and request a clean session.
Lastly, we set the QOS for publishing and subscribing to qos-1 using mqtt/with-publish-qos.
Within these three nested contexts, setting up a client, a connection, and QOS, we can subscribe to topics, send messages, and wait for messages to be received. Here, we use mqtt/subscribe to subscribe to the topic "some-topic". We use mqtt/publish to publish the message "Hello World". Finally, we receive a message using mqtt/with-message-recv .
The form mqtt/with-message-recv creates its own context, in which two named variables are defined designating the topic name on which the message was received and the message payload. Here, we call these variables topic and payload and display them.
#:clean-session#t)
The MQTT API can be used to exchange messages via MQTT in a functional style where the state of the application is managed in the form of scopes and construction and destruction of stateful objects is implicit.
predicate
x:any/c
predicate
( mqtt/will? x)→boolean?
x:any/c
This module manages MQTT connections using context forms. A context form is a form consisting of a head in which parameters are set and a body that is enriched by a context that adheres to the aforementioned parameters.
Two context forms are necessary to create a connection to an MQTT message broker: mqtt/with-client and mqtt/with-connection . Herein, mqtt/with-client prepares an MQTT client by fixing the message broker URI and the client id. mqtt/with-connection , in turn, configures a concrete connection allowing the user to set parameters like the keep-alive interval, or the will.
In addition, there are two optional context forms: mqtt/with-qos to set the QOS and mqtt/with-timeout to set the timeout for publishing and receiving.
syntax
( mqtt/with-client (server-uriclient-id)body...)
server-uri : string?client-id : string?persist-dir : path-string?body : any/c
If persist-dir is given, then the client state is persisted in that directory instead of in-memory.
syntax
( mqtt/with-connection (binding...)body...)
binding = #:keep-alive-intervalkeep-alive-interval| #:clean-sessionclean-session| #:reliablereliable| #:willwill| #:usernameusername| #:passwordpassword| #:connect-timeoutconnect-timeout| #:retry-intervalretry-interval| #:mqtt-versionmqtt-version| #:max-inflight-messagesmax-inflight-messages| #:clean-startclean-start| #:http-proxyhttp-proxy| #:https-proxyhttps-proxykeep-alive-interval : exact-nonnegative-integer?clean-session : boolean?reliable : boolean?connect-timeout : exact-nonnegative-integer?retry-interval : exact-nonnegative-integer?mqtt-version : mqtt-version?max-inflight-messages : exact-integer?clean-start : boolean?body : any/c
syntax
( mqtt/with-timeout (timeout)body...)
timeout : exact-positive-integer?body : any/c
syntax
( mqtt/with-qos (qos)body...)
procedure
( mqtt/subscribe topic)→void?
topic:string?
syntax
( mqtt/with-message-recv (topicpayload)body...)
topic = idpayload = idbody : any/c