MQTT Essentials Special: MQTT over WebSockets
Welcome to the MQTT Essentials, a blog series about the core features and concepts in the MQTT protocol. The last 10 posts have covered all important aspects and features of MQTT. This week we explain how you can use all of these features in the browser as well and what’s the benefit of it.
MQTT over WebSockets
We have learned in the MQTT Essentials that MQTT is ideal for constrained devices and unreliable networks. It’s also perfect for sending messages with a very low overhead. It would be quite nice to send and receive MQTT messages directly in the browser of a mobile phone or in general. This is possible by MQTT over WebSockets. It enables the browser to leverage all MQTT features and this can be used for the following example use case:
- Display live information from a device or sensor
- Receive push notifications, for example if there is an alert or critical condition
- See the current status of devices with LWT and retained messages
- Communicate efficiently with a mobile web application
What does it mean from a technical point of view?
How does it work?
WebSocket is a network protocol that provides bi-directional communication between a browser and a web server. It was standardized in 2011 and all modern browsers have support for WebSockets built-in. Similar to MQTT, WebSockets are based on TCP.
When using the term MQTT over WebSockets, it means that a MQTT message, for example CONNECT or PUBLISH is encapsulated by one or more WebSocket frames, when transferring over the network. WebSockets are suitable as transport for MQTT because the communication is bi-directional, ordered and lossless (which is essentially because WebSockets also leverage TCP). In order to communicate with an MQTT broker over WebSockets, the broker must be able to handle native WebSockets. Brave people can also use a webserver and bridge the WebSockets to the MQTT broker, we don’t recommend this, though. When using HiveMQ it is very easy to get started with WebSockets, just enable the native support in the configuration, more information can be found in the blog post MQTT over WebSockets with HiveMQ.
Why not use MQTT directly?
At the moment it is not possible to speak pure MQTT within a browser, because there is no possibility to open a raw TCP connection. This will eventually change with the Socket API, but at the moment there are only a few browsers implementing this API.
If you want to get started with MQTT over WebSockets there are a few good resource you should check out:
- We have a MQTT WebSocket client, which is ideal for testing, testing and debugging. It uses the public broker of the MQTT Dashboard as default broker. All the features of the client are documented in detail in this blog post. The source code is also available at GitHub.
- There is another blog post with a step-by-step guide on how to build your own MQTT WebSockets client. This one is a little less fancy and ideal if you want to integrate MQTT in your existing web application.
- If you want to setup your own broker with WebSockets support have a look at this post.
It is also possible to use secure Websockets with encryption of the whole connection by leveraging TLS. This works seamlessly with HiveMQ as well, there are some great explanations in our support forum here and here if you struggle to get started with secure WebSockets.
So that’s the end of the MQTT Essentials series. We hope you enjoyed it. No worries, the MQTT Monday will continue, we already have a lot of great ideas for the upcoming weeks, so stay tuned for more helpful content about MQTT and HiveMQ.
Have a great week and we’ll hope to see you on the next MQTT Monday!
You want to read more blog post related to MQTT? Then sign up for our newsletter and get notified on each new post as soon as its available. If you prefer RSS, you can subscribe to our RSS feed here.