In the previous tutorial, basics of MQTT-SN protocol were discussed. It was mentioned that an MQTT-SN Gateway needs to communicate with an MQTT-SN Broker. The MQTT-SN brokers are little different from MQTT Brokers. Really Small Message Broker is one of the popular MQTT-SN brokers.
Fig. 1: Image showing RSMB Broker for MQTT-SN
Really small message broker (aka RSMB) is a light-weight, low-overhead messaging MQ telemetry transport broker (version 3 or 3.1) or MQTT-SN broker. It is a pub/sub based broker. It allows messaging to and from tiny devices such as sensors and actuators over networks that are constrained in terms of low bandwidth, limited processing capabilities and varying reliability. Publisher sends message to the broker which then distributes the messages to the “subscribers” who have requested to receive those messages.
RSMB broker is similar to the other MQTT brokers (HiveMQ, Mosquitto, Adafruit) but the difference is that it provides connectivity to other brokers also in parallel. It creates a “bridge” that allows connections to other MQTT brokers. This broker allows message to be passed between RSMB instances as well as other MQTT servers. The RSMB broker also supports MQTT-SN protocol which when implemented accepts the UDP packets and sends those packets to local MQTT broker and bridged MQTT brokers (the one who has subscription).
RSMB broker takes up only 50 KB of storage space and can run in only 150 KB or less of memory which makes it ideal for installing and running on small servers and embedded devices. The RSMB broker by default run on 1883 port over MQTT. It need to be configured to allow the connection for user defined ports.
Installing and Running RSMB Broker –
The RSMB broker can be installed on the following operating systems –
1) Linux for Intel 32-bit kernel 2.6.8 or above
2) Linux for Intel 64-bit kernel 2.6.8 or above
3) Linux for ARM XScale, kernel 2.0.0 or above (Crossbow Stargate or Eurotech Viper)
4) Windows XP
5) Apple Mac OS X Leopard
6) Raspbian OS
For installing the broker on windows, follow the below steps –
1) Download the zip file for the platform.
2) Extract the zip file to a location on the computer.
3) On Windows, install Microsoft Visual C++ 2008 Redistributable Package, if it is not already installed.
4) Run the broker by clicking on broker.exe.
For installing the broker on Linux or Raspbian, follow the below steps –
1) Get clone of the RSMB broker.
2) Change directory to : cd mosquito.rsmb/rsmb/src
3) Run make
4) To run the broker, run ./broker for mqtt and ./broker_mqtts for mqtt-sn. To run the broker in background, run the command nohup ./broker >> /dev/null
5) The broker is now live on the local machine. By default it listens to 1883.
6) To kill the broker, type Ctrl+C. The process of the running broker can be killed by passing the following commands –
Fig. 2: Screenshot of Command to Kill the Broker
How RSMB Broker Works –
The RSMB broker by default listens for MQTT connection on port 1883. The default behavior can be changed by the configuration file. The RSMB broker can create a bridge between MQTT and MQTT-SN. It listens for MQTT packets over TCP/IP stack and MQTT-SN packets over UDP stack. The MQTT-SN clients send the message on UDP stack with topic ID to the RSMB broker. The RSMB broker saves that message in its buffer as well as forwards the messages to those bridged MQTT client who have subscribed the same topic ID.
RSMB Broker Configuration –
The broker by default does not have any configuration file. But the configuration file can be added to command the broker to listen on particular port with the particular network address. The configuration file is also used to control other attributes of the broker by passing the configuration parameters. Each configuration parameter is defined in standard format with a separate line as follow –
Fig. 3: Screenshot of Configuration Parameter for RSMB Broker
The lines starting with “#” are treated as comments, and are ignored by the broker. When the broker starts, it looks for a configuration file called broker.cfg in the same directory as the broker is installed (on Windows, for example, the same directory as the broker.exe file).
When the broker is run from the command line, the name and location of the configuration file can be specified as a parameter. For example, if the configuration file is called broker.cfg, enter the following command to start the broker –
Fig. 4: Screenshot of Command to Start the Broker
A mqtts_conf.cfg configuration file looks like the following –
Fig. 5: Screenshot of mqtts_conf.cfg Configuration File
The Configuration file has the following parameters defined –
trace_output: A destination to write trace entries as they occur.
Listener: A listener is a process which waits for connections from clients. It has a specific port and network address.
Connection: Any connection name. It is the name which is given to identify the unique connection. The connection name must be alphanumeric and must not contain any spaces.
Address: The port is the IP address of the computer that hosts the remote broker and the port number that the remote broker uses. By default the port is 1883. If no port number is specified, specify the IP address without the colon (:).
Topic # out: It represents the topic hierarchy on which the local broker will publish the message to the remote broker over the bridge. # out means whatever message on particular topic RSMB broker will receive, it will pass it to the MQTT broker. A particular topic can also be specified so that only registered topic will be used to publish or subscription.
Bridging between two Brokers –
It can be seen that in the configuration file, the bridge connection between RSMB and Mosquitto has been specified. Both are running on the same machine. These two brokers can run independently on different machines also. For this configuration file needs to be changed. The MQTT broker IP address and the port number need to be specified in the configuration file for bridging. A broker can start listening on different ports for same machine as well. The Client can access the same server with different ports. The connections can be limited on the server end as well.
The server can be secured by setting the username and password at the end of server. Then, only the authenticated clients can access the server. The security can be enabled by limiting the connections to only specific clients IDs. The other way to provide the security at both client end and server end can be enabled by generating the certificates for both client and server so that the client will communicate only with broker if it will have the server certificate and vice-versa.
In the next tutorial, learn to setup Client-Server Communication over MQTT-SN Protocol using RSMB Broker.
Filed Under: Featured Contributions