Mosquitto Broker with Websockets enabled on the Synology NAS

Synology NAS are great devices, mainly due to the bundled software (CloudStation, Photo Station, to name a few), but also because it can run other software that is provided on non official package sources.
One of such providers is the SynoCommunity that provides a Mosquitto MQTT broker packaged for the Synology NAS. While I’ve successfully was able to cross compile the Mosquitto broker for my DS212+ NAS with websockets enabled (I do have a draft post with the steps that I’ve never published), the SynoCommunity package allows a simpler way to install and use a Mosquitto broker that has the WebSockets protocol support compiled in.

So, what is needed to be done?

Setting up:

  • 1st: Add to your Synology server the SynoCommunity package source. See the detailed instructions on the community web page.
  • 2nd: Install the Mosquitto package from the Synology package manager.
  • 3rd: Edit the file located at /usr/local/mosquitto/var/mosquitto.conf. You need to connect through ssh as root to do this.

At the end of this file add the following lines:

listener 1883
listener 9001
protocol websockets

Note that the 1883 is the standard MQTT broker port, and 9001 is the port that I’ve choose have the websockets server listening/answering.

After the change, stop and start the Mosquitto broker on the package manager action dropdown box for the Mosquitto package.

Still using ssh, and logged in as root, check now that the port 9001 is active:

root@DiskStation:/volume1/homes/root# netstat -na | grep 9001 
tcp        0      0 0.0.0.0:9001            0.0.0.0:*               LISTEN       
tcp        0      0 192.168.1.250:9001      192.168.1.32:55331      ESTABLISHED

In the above case we can see that there is a websocket client connected to the broker from my workstation.

Testing:

For testing we can use the HiveMQ websocket client: http://www.hivemq.com/demos/websocket-client/ that allows to communicate using the websockets transport.

Despite the fact that the page is available on an external, internet based server, the websocket client will be running on YOUR web browser, on your machine in your own (internal) network, so at the connection settings for the broker, for Host I use my internal IP address: 192.168.1.250, and for the Port: I use the 9001 value.

I can now subscribe and publish to topics using the browser, and also using MQTT-SPY we can see and check the MQTT Websockets communication.

And that’s it!

Advertisements

14 thoughts on “Mosquitto Broker with Websockets enabled on the Synology NAS

  1. Too bad, It doesn’t look the port is opened

    NAS> netstat -na | grep 9001
    tcp 0 0 0.0.0.0:9001 0.0.0.0:* LISTEN
    NAS>

    Running Mosquitto 1.4.2-4
    Synology: DSM: 5.2-5644 Update 1

  2. Pingback: Mosquitto mqtt Message Broker auf der Synology | webdevign

  3. Thanks for the info! When I try to add “protocol websockets” to my mosquitto.conf file, my mosquitto service refuses to start. Any ideas on what could be wrong? (I’m on mosquitto 1.4.2-3, no newer version is shown in my package manager)

  4. Thanks for the write up. Works a treat. Do you know how to add a username and password to the Mosquitto instance as I get ‘-ash: mosquitto_passwd: not found’ when I run the standard Mosquitto command?

    • Hi, the mosquitto_passwd is not in the path, hence the shell fails to find it.

      Have a look at the following:

      root@DiskStation:/volume1/@appstore/mosquitto/bin# pwd
      /volume1/@appstore/mosquitto/bin
      root@DiskStation:/volume1/@appstore/mosquitto/bin# ./mosquitto_p
      mosquitto_passwd mosquitto_pub
      root@DiskStation:/volume1/@appstore/mosquitto/bin# ./mosquitto_passwd
      mosquitto_passwd is a tool for managing password files for mosquitto.

      Usage: mosquitto_passwd [-c | -D] passwordfile username
      mosquitto_passwd -b passwordfile username password
      mosquitto_passwd -U passwordfile
      -b : run in batch mode to allow passing passwords on the command line.
      -c : create a new password file. This will overwrite existing files.
      -D : delete the username rather than adding/updating its password.
      -U : update a plain text password file to use hashed passwords.

      See http://mosquitto.org/ for more information.

      root@DiskStation:/volume1/@appstore/mosquitto/bin#

  5. websockets now work, but now normal mqtt port/protocol doesn’t work anymore?! Can’t both protocols be run (on different ports)?

    • Make sure there are no blank lines between the Listener and the protocol directives. Both work at the same time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s