Enabling Mosquitto websockets on Synology NAS

Websockets are cool. They are the modern sibling of http in that they run over tcp, but "EthernetCableBlue2" by Raysonho @ Open Grid Scheduler / Grid Engine - Own work. Licensed under CC0 via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:EthernetCableBlue2.jpg#/media/File:EthernetCableBlue2.pngwebsockets offer a lot more, most notably full duplex (i.e. data can be sent in both directions) and realtime delivery of messages.

Those two features enable the creation of web pages that update dynamically as soon as new data is available on the server. No need to reload the web page in the user’s browser.

I have been struggling with how to get websockets integrated with MQTT on my Synology DS1515+ NAS, but in the end it turned out to be pretty easy!

MQTT is another very cool concept/protocol. Loosely related to IBM’s Messaging Queue (MQ) protocol, MQTT focuses on messaging between small footprint devices (low processing power, IoT etc). While it can in some cases be used to keep information about system state, it is in the machine-to-machine communication area the protocol really shines.

Mosquitto is one of the leading open source MQTT implementations. It runs on most platforms, from small devices like Raspberry Pi, as well as Windows, OS X and most major (a few more niche) Linux distributions. I have been using it on and off for a year or two, with a lot more done through MQTT/Mosquitto during the past 6 months. It has in many ways become the nervous system in the home automation and monitoring system at home here – everything from weather forecasts, frost alerts, temperature and energy usage monitoring, status of computers and network devices, arrival/departures of people etc etc – all kinds of data lives in MQTT in a structured, easy to get to, way.

And here comes the good part: As of version 1.4 of Mosquitto, web sockets are natively supported.

This opens up to some pretty cool possibilities. For example, it is now possible to create live web pages that always show the latest temperature/energy/forecast/presence/… information – all retrieved from a single source (rather than all sorts of different databases, REST APIs, external web services etc). Very powerful indeed!

Thanks to the great work by the SynoCommunity project, Mosquitto is available for many Synology NAS models, current version as of this writing is 1.4.2-4. Websockets are however not enabled by default, it is however very easy to do:

  1. Enable downloads from the SynoCommunity repository, as described on their site.
  2. Install Mosquitto, verify it is running. It will by default listen on port 1883, you can use a tool like MQTTLens (available on Chrome app store) or similar tools to verify that your newly
    installed MQTT broker is working. Just connect the MQTT client to your Synology’s IP address on port 1883, and you should be able to send and receive MQTT messages.
  3. ssh into your NAS as root, or as a regular user and then switch to root. Enabling SSH on Synology NASYou will need to enable ssh in the NAS control panel to do this. You should consider using a non-standard port (i.e. not 22), as it will at least somewhat decrease the number of attacks you will see on the port (if your NAS is open to the Internet). You should of course also have strong passwords, public key pairs to log in etc etc…
  4. Let’s find out where the Mosquitto config file lives:
    DS1515P> find / -name mosquitto.conf
    /volume1/@appstore/mosquitto/var/mosquitto.conf
    DS1515P>
  5. Edit the “Extra listeners” section of the Mosquitto config file. The VI editor is installed by default in the BusyBox shell (no VIM, Nano or similar – sorry..) so it reads something like this:
    # =================================================================
    # Extra listeners
    # =================================================================
    
    # Listen on a port/ip address combination. By using this variable
    # multiple times, mosquitto can listen on more than one port. If
    # this variable is used and neither bind_address nor port given,
    # then the default listener will not be started.
    # The port number to listen on must be given. Optionally, an ip
    # address or host name may be supplied as a second argument. In
    # this case, mosquitto will attempt to bind the listener to that
    # address and so restrict access to the associated network and
    # interface. By default, mosquitto will listen on all interfaces.
    # listener port-number [ip address/host name]
    #listener
    listener 9001
    
    # The maximum number of client connections to allow. This is
    # a per listener setting.
    # Default is -1, which means unlimited connections.
    # Note that other process limits mean that unlimited connections
    # are not really possible. Typically the default maximum number of
    # connections possible is around 1024.
    #max_connections -1
    
    # The listener can be restricted to operating within a topic hierarchy using
    # the mount_point option. This is achieved be prefixing the mount_point string
    # to all topics for any clients connected to this listener. This prefixing only
    # happens internally to the broker; the client will not see the prefix.
    #mount_point
    
    protocol websockets
  6. Sample Mosquitto config files are found in the /usr/local/mosquitto/etc/mosquitto/ directory, they are very useful if you want to add additional features like security to your Mosquitto setup.
  7. Restart Mosquitto from the package center app in the Synology web UI, and you are good to go. You now have a websockets enabled MQTT server.

 

More to come on this topic, I suspect.. got some neat ideas brewing.

 

If you want to read up on MQTT and Mosquitto, I recommend Jan-Piet Mens’ blog, lots of great posts about MQTT, advanced DNS usage and other goodness.

 

 

9 thoughts on “Enabling Mosquitto websockets on Synology NAS

  1. Michiel

    Why the line “listener 9001”?
    It works for me with the default port 1883.

    At first, I logged in as “admin”, then found the file, but vi does not allow me to save this read only file.
    Solution: log in as root with the admin password…

    Reply
    1. mountaindude Post author

      Good questions.
      The listener on port 9001 was needed as I had other, non websockets MQTT connections on port 1883.
      So I needed a new port for the websockets connections, I think someone mentioned somewhere that 9001 was the one to use… Any free port should work though, I suspect.

      And yes, you prob need to be root to do this – don’t quite remember – but that does seem reasonable.

      Reply
  2. Sharp6

    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)

    Reply
  3. Jan

    Well I added the extra lines to conf and now websockets on 9001 work but non-websockets mqtt port on 1883 doesn’t anymore?! Can’t both run?

    Reply
  4. Hugo

    Thanks for this post … i was looking for a mqtt broker/server for my virtual machine
    but the idea to have it on my synology is even better.
    but …. it does not run on my ds1512+
    i can edit the mosquito.conf as root.
    but the mosquito package will not run (on my dsm 6 update 4 ) .. nor comes there a entry in the log

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *