8.16.0.1
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.
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 identifying a quality of service (QOS). Either one of three symbols: 'qos-0,
'qos-1, or 'qos-2.
Predicate identifying an MQTT version. Either one of four symbols: 'mqtt-version-default,
'mqtt-version-3-1, 'mqtt-version-3-1-1, or 'mqtt-version-5.
Predicate identifying a will object created with
mqtt/will.
2.2 Connections🔗ℹ
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.
Context form, initializing an MQTT client communicating to a message broker identified by the
server-uri. The
client-id is a unique label the client gives itself. The body
can be any kind and any number of Racket expressions including an
mqtt/with-connection
context form.
If persist-dir is given, then the client state is persisted in that directory instead
of in-memory.
(mqtt/with-connection (binding ...) body ...)
|
|
binding | | = | | #:keep-alive-interval keep-alive-interval | | | | | | #:clean-session clean-session | | | | | | #:reliable reliable | | | | | | #:will will | | | | | | #:username username | | | | | | #:password password | | | | | | #:connect-timeout connect-timeout | | | | | | #:retry-interval retry-interval | | | | | | #:mqtt-version mqtt-version | | | | | | #:max-inflight-messages max-inflight-messages | | | | | | #:clean-start clean-start | | | | | | #:http-proxy http-proxy | | | | | | #:https-proxy https-proxy |
|
|
| mqtt-version | | : | | mqtt-version? |
|
Context form to set up an MQTT connection. Must be used in the body of a client context
created using
mqtt/with-client. Setting the will requires creating a will object using
mqtt/will.
Context form for setting the timeout for publishing and receiving operations in milliseconds.
Specifically, the expressions
mqtt/publish and
mqtt/with-message-recv use
this timeout value. If the context form is omitted, the timeout defaults
to
15000 milliseconds.
Context form for setting the QOS for publishing. If the
mqtt/with-qos form is
omitted, the QOS defaults to
'qos-2.
Publish a message
payload to the given
topic. Optionally, the message can be
flagged as retained using the keyword argument
#:retained which defaults to
#f.
The QOS is set to the value defined with
mqtt/with-qos.
2.4 Subscriptions🔗ℹ
Subscribe the client to the given
topic. The QOS is set to the value defined with
mqtt/with-qos.
Context form for receiving a message from the message broker. In the body of the form the variables
identified by topic and payload are defined both having string values.