Skip to content

MQTT over WebSockets - MQTT Essentials Special

by HiveMQ Team
7 min read

Welcome to MQTT Essentials. In the last 10 posts of this series, we’ve covered the core concepts and features of MQTT. In this blog, we will explain how you can use all of those great features in a browser (and what the benefits of doing so are).

MQTT over WebSockets

We’ve seen that MQTT is ideal for constrained devices and unreliable networks and that it is perfect for sending messages with very low overhead. Naturally, it would be quite nice to send and receive MQTT messages directly in a browser. For example, on a mobile phone. MQTT over WebSockets is the answer. MQTT over WebSockets enables the browser to leverage all MQTT features. You can use these capabilities for many interesting use cases:

  • Display live information from a device or sensor.

  • Receive push notifications (for example, an alert or critical condition warning).

  • See the current status of devices with LWT and retained messages.

  • Communicate efficiently with mobile web applications.

What does all this mean from a technical point of view?

Every modern browser that supports WebSockets can be a full-fledged MQTT client and offer all the features described in the MQTT Essentials. The Keep Alive, Last Will and Testament, Quality of Service (QoS), and Retained Messages features work the same way in the browser as in a native MQTT client. All you need is a JavaScript library that enables MQTT over WebSockets and an MQTT broker that supports MQTT over WebSockets. Of course, the HiveMQ broker offers this capability straight out-of-the-box.

How Does MQTT Over WebSockets Work?

WebSocket is a network protocol that provides bi-directional communication between a browser and a web server. The protocol was standardized in 2011 and all modern browsers provide built-in support for it. Similar to MQTT, the WebSocket protocol is based on TCP.

MQTT over WebSocketsIn MQTT over WebSockets, the MQTT message (for example, a CONNECT or PUBLISH packet) is transferred over the network and encapsulated by one or more WebSocket frames. WebSockets are a good transport method for MQTT because they provide bi-directional, ordered, and lossless communication (WebSockets also leverage TCP). To communicate with an MQTT broker over WebSockets, the broker must be able to handle native WebSockets. Occasionally, people use a webserver and bridge WebSockets to the MQTT broker, but we don’t recommend this method. When using HiveMQ, it is very easy to get started with WebSockets. Simply enable the native support in the configuration. For more information, read MQTT over WebSockets with HiveMQ.

Why not use MQTT directly?

Currently, it is not possible to speak pure MQTT in a browser because it is not possible to open a raw TCP connection. Socket API will change that situation; however, few browsers implement this API yet.

Get Started with MQTT Over WebSockets

If you want to get started with MQTT over WebSockets, here are some useful resources:

Secure WebSockets

You can leverage Transport Layer Security (TLS) to use secure WebSockets with encryption of the whole connection. This method works seamlessly with HiveMQ. However, there are a few points that you need to keep in mind. For more information, read our user guide on Listeners.

HiveMQ Team

The HiveMQ team loves writing about MQTT, Sparkplug, Industrial IoT, protocols, how to deploy our platform, and more. We focus on industries ranging from energy, to transportation and logistics, to automotive manufacturing. Our experts are here to help, contact us with any questions.

Related content:

HiveMQ logo
Review HiveMQ on G2