1. mosquitto
    1. Description
    2. Author
    3. Repository
    4. Requirements
    5. Example
    6. API
      1. Callbacks
    7. Caveats


Chicken Scheme Bindings to mosquitto MQTT client library


Dmitrii Kosenkov



Requires the following extensions:


(let ((client (make-mqtt-client #:on-connect
                                (lambda (client err)
                                  (if err
                                      (abort err)
                                      (display "Yay, we are connected!"))))))
  (set-mqtt-client-disconnect-callback! client
                                        (lambda (cl unexpected?)
                                          (when unexpected?
                                            (display "Unexpected disconnect..."))))

  (set-mqtt-client-message-callback! client
                                     (lambda (cl msg)
                                       (display (string->append
                                                 "Topic: " (mqtt-message-topic msg)
                                                 "Payload:" (blob->string (mqtt-message-payload msg))))
                                       (mqtt-publish client "topic2" "message received, thanks!" )))
  (mqtt-connect client "localhost" #:username "mqtt-admin" #:password "mypass")
  (mqtt-subscribe client "topic1")
  (mqtt-loop-forever client))


[procedure] (make-mqtt-client #!key id (clean-session #t) user-data on-connect on-disconnect on-publish on-message on-subscribe on-unsubscribe on-log)
[procedure] (mqtt-connect client host #!key (port 1883) (keepalive 5) bind-address username password tls-cafile tls-capath tls-certfile tls-keyfile tls-insecure tls-ocsp-required tls-use-os-certs tls-alpn socks5-host (socks5-port 1080) socks5-username socks5-password (reconnect-delay 1) (reconnect-delay-max 10) reconnect-exp-backoff tcp-nodelay)
[procedure] (mqtt-disconnect client)

Disconnect from the broker.

[procedure] (mqtt-loop client #!optional (timeout 1000))

The main network loop for the client. This must be called frequently to keep communications between the client and broker working. This is carried out by mqtt-loop-forever, which are the recommended ways of handling the network loop. It must not be called inside a callback. If incoming data is present it will then be processed. Outgoing commands, from e.g. mqtt-publish, are normally sent immediately that their function is called, but this is not always possible. mqtt-loop will also attempt to send any remaining outgoing messages, which also includes commands that are part of the flow for messages with QoS>0.

[procedure] (mqtt-loop-forever client #!optional (timeout 1000))

This function call mqtt-loop for you in an infinite blocking loop. It is useful for the case where you only want to run the MQTT client loop in your program. It handles reconnecting in case server connection is lost. If you call mqtt-disconnect in a callback it will return.

[procedure] (mqtt-publish client topic payload #!key (qos 0) retain)

Publish a message on a given topic.


[procedure] (mqtt-subscribe client sub #!key (qos 0))

Subscribe to a topic.

[procedure] (mqtt-unsubscribe client sub)

Unsubscribe from a topic.


[procedure] (set-mqtt-client-connect-callback! client (lambda (client err) ...))

Set the connect callback. This is called when the broker sends a CONNACK message in response to a connection.

[procedure] (set-mqtt-client-disconnect-callback! client (lambda (client unexpected?) ...))

Set the disconnect callback. This is called when the broker has received the DISCONNECT command and has disconnected the client.

[procedure] (set-mqtt-client-publish-callback! client (lambda (client mid) ...))

Set the publish callback. This is called when a message initiated with mosquitto_publish has been sent to the broker successfully.

[procedure] (set-mqtt-client-message-callback! client (lambda (client message) ...))

Set the message callback. This is called when a message is received from the broker.

[procedure] (set-mqtt-client-subscribe-callback! client (lambda (client mid) ...))

Set the subscribe callback. This is called when the broker responds to a subscription request.

[procedure] (set-mqtt-client-unsubscribe-callback! client (lambda (client mid) ...))

Set the unsubscribe callback. This is called when the broker responds to a unsubscription request.

[procedure] (set-mqtt-client-log-callback! client (lambda (client level str) ...))

Set the logging callback. This should be used if you want event logging information from the client library.