(net mq mqtt) - MQTT library

Library (net mq mqtt)

Providing MQTT v3.1.1 and partially v3.1 client APIs.

Reference OASIS MQTT.

Following examples describe how to receive and publish messages.

(import (rnrs) (net mq mqtt))

(let ((conn (open-mqtt-connection "localhost" "1883")))
  ;; subscribes to "topic" topic with accepting QoS exactly once
  (mqtt-subscribe conn "topic" +qos-exactly-once+
                  (lambda (topic payload)
                    (let ((msg (get-bytevector-all payload)))
                      (cond ((not (eof-object? msg))
                             (print (utf8->string msg))
                             (string=? (utf8->string msg) "END"))
                            (else #f)))))
  (let loop ()
    ;; receives until "END" message was sent
    (unless (mqtt-receive-message conn)
      (loop)))
  ;; unsubscribe from "topic"
  (mqtt-unsubscribe conn "topic")
  (close-mqtt-connection! conn))
(import (rnrs) (net mq mqtt))

(let ((conn (open-mqtt-connection "localhost" "1883")))
  ;; publish message to "topic" topic.
  (mqtt-publish conn "topic" (string->utf8 "Hello MQTT"))
  (close-mqtt-connection! conn))
Function mqtt-connection? obj

Returns #t if given obj is MQTT connection. Otherwise #f.

Function open-mqtt-connection host port opts ...

Creates a socket connected to host:port and pass it to port->mqtt-connection with opts.

The returning value is an MQTT connection object.

Function port->mqtt-connection in/out :key client-id username password keep-alive version

in/out must be a binary input/outport port.

Creates an MQTT connection object using in/out.

client-id, username, password and _keep-alive_keyword arguments are for optional payload of CONNECT packet. If they are given, then first 3 must be strings and keep-alive must be an integer.

version keyword argument is switches which version it should use. The value must be one of the followings;

Constant +mqtt-3.1+
Constant +mqtt-3.1.1+

By default it uses +mqtt-3.1.1+.

This procedure is for future extension such as supporting websocket.

Function close-mqtt-connection! conn

Closes given MQTT connection.

Function mqtt-subscribe conn topic qos callback

Subscribes to given topic with QoS qos.

callback must be a procedure and accept 2 arguments. topic and payload. payload is an binary input port.

To receive messages, use mqtt-receive-message.

Function mqtt-receive-message conn

Receives one message from one of subscribed topics and call registered callback.

Function mqtt-unsubscribe conn topic

Unsubscribes conn from topic.

Function mqtt-publish conn topic message :key qos

Publishes application message message to topic.

The topic must be a string. The message must be a bytevector.

If keyword argument qos is specified, it must be one of the followings.

Constant +qos-at-most-once+
Constant +qos-at-least-once+
Constant +qos-exactly-once+

By default, it uses +qos-at-most-once+.

Function mqtt-ping conn

Sends PINGREQ packet to the server.

MQTT broker

This library provides simple MQTT broker implementation.

The broker only does what broker suppose to do, thus there is no user customised behaviour.

The simplest broker script would look like this:

(import (rnrs) (net mq mqtt broker))

;; Wait on port 9000
(define broker (make-mqtt-broker "9000")))

;; start the broker.
(mqtt-broker-start! broker)

Creates MQTT broker.

The returning broker is a sub type of <simple-server>.

If config keyword argument is specified, the value must be an configuration object created by make-mqtt-broker-config, then the specified configuration is used. Otherwise default configuration which can be created by (make-mqtt-broker-config) is used.

If authentication-handler keyword argument is specified, then the specified value which must be a procedure takes 2 arguments, username and password, handles authentication. If the procedure doesn't return true value then authentication error packet is sent to the client.

Creates a MQTT broker configuration object.

The returning value is a sub type of <server-config> with :non-blocking? option.

Start and stop procedure for MQTT broker.

These procedures are mere redefinitions of server-start! and server-stop!.