mqtt-client
1 Example
2 MQTT API
2.1 Predicates
mqtt/  qos?
mqtt/  mqtt-version?
mqtt/  will?
2.2 Connections
mqtt/  with-client
mqtt/  with-connection
mqtt/  will
mqtt/  with-timeout
mqtt/  with-qos
2.3 Publishing
mqtt/  publish
2.4 Subscriptions
mqtt/  subscribe
mqtt/  with-message-recv
8.12

mqtt-client🔗ℹ

Jörgen Brandt

 (require mqtt-client) package: mqtt-client

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.

1 Example🔗ℹ

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.

#lang racket/base
 
(require mqtt-client)
 
(mqtt/with-client ("localhost" "client1")
 
  (mqtt/with-connection (#:keep-alive-interval 20
                         #:clean-session       #t)
 
    (mqtt/with-qos ('qos-1)
 
      (mqtt/subscribe "some-topic")
 
      (mqtt/publish "some-topic" "Hello world")
 
      (mqtt/with-message-recv (topic payload)
        (displayln topic)
        (displayln payload)))))

2 MQTT API🔗ℹ

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.

2.1 Predicates🔗ℹ

predicate

(mqtt/qos? x)  boolean?

  x : any/c
Predicate identifying a quality of service (QOS). Either one of three symbols: 'qos-0, 'qos-1, or 'qos-2.

predicate

(mqtt/mqtt-version? x)  boolean?

  x : any/c
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

(mqtt/will? x)  boolean?

  x : any/c
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.

syntax

(mqtt/with-client (server-uri client-id) body ...)

(mqtt/with-client (server-uri client-id persist-dir) body ...)
 
  server-uri : string?
  client-id : string?
  persist-dir : path-string?
  body : any/c
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.

syntax

(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
 
  keep-alive-interval : exact-nonnegative-integer?
  clean-session : boolean?
  reliable : boolean?
  will : (or/c false? mqtt/will?)
  username : (or/c false? string?)
  password : (or/c false? string?)
  connect-timeout : exact-nonnegative-integer?
  retry-interval : exact-nonnegative-integer?
  mqtt-version : mqtt-version?
  max-inflight-messages : exact-integer?
  clean-start : boolean?
  http-proxy : (or/c false? string?)
  https-proxy : (or/c false? string?)
  body : any/c
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.

constructor

(mqtt/will topic    
  message    
  [#:retained retained])  mqtt/will?
  topic : string?
  message : string?
  retained : boolean? = #f
Constructor to create a will object to be used in the head of a mqtt/with-connection context form. The QOS is set to the value defined with mqtt/with-qos.

syntax

(mqtt/with-timeout (timeout) body ...)

 
  timeout : exact-positive-integer?
  body : any/c
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.

syntax

(mqtt/with-qos (qos) body ...)

 
  qos : (or/c qos? 0 1 2)
  body : any/c
Context form for setting the QOS for publishing. If the mqtt/with-qos form is omitted, the QOS defaults to 'qos-2.

2.3 Publishing🔗ℹ

procedure

(mqtt/publish topic    
  payload    
  [#:retained retained])  void?
  topic : string?
  payload : string?
  retained : boolean? = #f
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🔗ℹ

procedure

(mqtt/subscribe topic)  void?

  topic : string?
Subscribe the client to the given topic. The QOS is set to the value defined with mqtt/with-qos.

syntax

(mqtt/with-message-recv (topic payload) body ...)

 
topic = id
     
payload = id
 
  body : any/c
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.