What is Websockets and How it Works?
WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP/IP connection. Wiki
It is closely associated with http as it uses http for the initial connection establishment..
The client and server connect using http and then negotiate a connection upgrade to websockets, the connection then switches from http to websockets.
The client and server can now exchange full duplex binary data over the connection.
Video -MQTT Over Websockets Explained
Why Use MQTT over Websockets?
MQTT over Websockets allows you to receive MQTT data directly into a web browser.
This is important as the web browser may become the DE-facto interface for displaying MQTT data.
MQTT Over Websockets vs MQTT.
In the case of MQTT over Websockets the websockets connection forms an outer pipe for the MQTT protocol.
The MQTT broker places the MQTT packet into a websockets packet, and sends it to the client.
The client unpacks the MQTT packet from the websockets packet and then processes it as a normal MQTT packet.
This is illustrated in the diagram below:
With MQTT the MQTT Packet is placed directly into the TCP/IP Packet.
Websockets and Mosquitto
The default Mosquitto install packages for Windows and Linux do not include support for WebSockets.
If you want to test WebSockets with MQTT then you need to either:
- Compile and install your own Mosquitto broker with Websocket support.
- Use an oneline broker that has websocket support
Instruction Notes for http://goochgooch.co.uk/2014/08/01/building-mosquitto-1-4/
The instructions in the guide work. but I needed to make some slight changes as detailed here:
Couldn’t find libwebsockets-1.22-chrome26-firefox18.tar.gz so I used this instead libwebsocckets-1.7.8.tar.gz. Got it from here https://libwebsockets.org/git/libwebsockets/
There was no makefile so I had to create the build directory and run cmake.
The ares.h file was missing, so I needed to install it as per instructions.
I kept forgetting to run commands as sudo which meant copies failed when running make.
When I started the broker it couldn’t find libwebsockets.so.7 but running the command
Websockets on Windows
Since mosquitto 1.5.1 websockets support has been enabled on the windows binary files.
However when you start mosquitto it appears to be listening on the websocket port but doesn’t allow connections.
mosquitto v 1.5.4 does work with websockets. Here is a link for the downloads.
Configuring Websockets On Your Own Mosquitto Broker
MQTT over Websockets usually uses port 9001 but it isn’t fixed.
You need to make change to the mosquitto.conf file, by adding the following:
listener 9001 protocol websockets
This creates an extra listener using websockets and port 9001.
When you start the broker you should see something like this:
To test websockets you will need a client that supports websockets.
Websockets and the Python Client -Example
To tell the client to use websockets instead of MQTT use the command
instead of simply
When creating a new MQTT client object.
You also need to set the port to the WebSocket port. (9001).
Here is a Python demo script that you can use to publish and subscribe using websockets.
If you run the script you see:
There is no real indication that the client is using websockets as opposed to standard MQTT.
Using Websockets over TLS (SSL)
To use websockets over TLS you need to configure the broker to use TLS.
See this tutorial Mosquitto SSL Configuration -MQTT TLS Security.
Now we edit our mosquitto.conf file. It should look like this:
Notice The extra listener is using websockets and the ssl configuration applies to it. I also used port 8081.
On the Python client add the client.tls_set() command to tell it to use SSL as well as setting the transport.:
client= paho.Client("control1",transport='websockets') client.tls_set('c:/python34/steve/mqtt-demos/certs/ca.crt')
Using an External MQTT Broker with Websockets
If you can’t get websockets on your own broker then you can use as external one like :
- test.mosquitto.org– uses port 8080 un-encrypted and 8081 for websockets over SSL.
- iot.eclipse.org support only encrypted and uses port 443 for websockets over SSL..
- broker.hivemq.com -uses port 8000 for websockets. Websockets over SSL not supported.
Here is a list of other test brokers with Port numbers
The mosquitto_pub and mosquitto_sub tools do not support websockets.
The MQTT box (Google Chrome extension) tool supports both websockets and secure websockets. However you may have difficulty with SSL if you use your own CA.
MQTTLens supports websockets but not secure websockets.
Common Questions and Answers
Q- Can You use websockets over an encrypted connection?
A- Yes the one uses MQTT over Websockets and is meant to be run in a browser. The other uses MQTT and is meant for use with node.js.See Using the Node.js MQTT Client-Starting Guide
Related Tutorials and resources
- TCP/IP protocol suite explained
- Client Objects-Working with The Python MQTT Client
- MQTT-SN (MQTT for Sensor networks) Working Notes
- MQTT protocol Overview