The server implements MQTT messaging protocol version 3.1, 3.1.1, 5.0 and allows communicate with MQTT clients to send/receive messages. The server is written in Erlang. The server was tested with following clients
- Mosquitto command line tools subscriber and publisher
- MQTT.fx client
- Erlang MQTT client
Simple Instant messenger SIM was created to test functionality of the MQQT server and prove good performance of the code. Live demo of SIM is running here.
MQTT server is an OTP application. Application depends on other Erlang applications:
lager
for logging services,ranch
for tcp and tsl connections,msql_client
for connection to MySQL server,mqtt_common
that is library keeping code that is common for client and server implementation
Session state data is storing in database (DETS or MySQL in current version). Server can establish connection using different network protocols:
- clear it/tcp
- tls/ssl
- web socket
To start with the server you have to complete two steps below:
Download source code to local host. Type command
$ git clone https://git.code.sf.net/p/mqtt-server/code erl.mqtt.server
or
$ git clone https://github.com/alekras/erl.mqtt.server.git erl.mqtt.server
After you have got source code of the server then change directory to the erl.mqtt.server:
$ cd erl.mqtt.server
Run rebar3 for this project. You have to add path to rebar3 to OS PATH variable or just use the whole path:
$ /opt/local/bin/rebar3 compile
Rebar will fetch code of all dependencies and compile source files of main project and all dependencies.
To start server run bash script:
$ ./start_mqtt_server.sh
Erlang shell will open and log statements are appearing in console.
To make release of the application run command:
$ /opt/local/bin/rebar3 as prod release
Go to folder:
$ cd _build/prod/rel/mqtt_server
and run command to start server:
$ bin/mqtt_server console
You can test the server with any MQTT client complained with protocol version 3.1.1 or 5.0. I recommend to try Erlang MQTT client or client.
To test with Mosquitto tools you need to open two terminal windows. One for subscribing and other for publishing. Open the first terminal windows and change directory to folder where Mosquitto is installed:
$ cd /usr/local/Cellar/mosquitto/1.4.10/
Now subscribe to "test/c" topic:
$ bin/mosquitto_sub -t test/c -p 18883 -i test -u guest -P guest -V mqttv5
Open the second terminal windows and change directory to Mosquitto installation:
$ cd /usr/local/Cellar/mosquitto/1.4.10/
Publish some message to "test/c" topic:
$ bin/mosquitto_pub -t test/c -p 18883 -i test1 -u guest -P guest -m "Test message from mosquitto tools QoS=2" -q 2 -V mqttv5
In first terminal you will see incoming message:
$ Test message from mosquitto tools QoS=2
The server was tested with other clients:
- Websocket MQTT client from HiveMQ [http://www.hivemq.com/demos/websocket-client/].
- MQTT.fx client [http://www.mqttfx.org/].
To set up ports for TCP and TLS socket connection go to mqtt.config. This is OTP application configuration file contained startup data for lager, ranch and mqtt server configuration.
The MQTT server is distributed with preset user dets db. Users are guest/guest and admin/admin. To add other users run Erlang shell:
$ erl -pa _build/default/lib/*/ebin
and issue command in Erlang shell:
1>mqtt_server:add_user("UserName", <<"UserPassword">>).
or for user delete:
1>mqtt_server:remove_user("UserName").
- [https://mosquitto.org/] - Mosquitto MQTT server.
- [https://www.rabbitmq.com/] - RabbitMQ server with MQTT plugin.
- [https://sourceforge.net/projects/mqtt-client/] - Erlang MQTT client.
- [http://www.hivemq.com/demos/websocket-client/] - MQTT websocket client.
- [http://www.mqttfx.org/] - MQTT client.