MQTT over WebSockets - MQTT Essentials Special

MQTT over WebSockets - MQTT Essentials Special

author HiveMQ Team

Written by HiveMQ Team

Category: MQTT Essentials MQTT

Published: April 2, 2015

Welcome to MQTT Essentials, a blog series about the core features and concepts in the MQTT protocol. In the last 10 posts of this series, we’ve covered the core concepts and features of MQTT. This week, we 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 a 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:

Learn about MQTT Protocol

Get the MQTT Essentials eBook

72 pages of MQTT education and learning for beginners and experts alike.

  • 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, 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 a broker that supports MQTT over WebSockets. Of course, the HiveMQ broker offers this capability straight out-of-the-box.

How does it 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.


In 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

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, see the Gotcha section of our user guide.

That brings us to the end of the MQTT Essentials series. We hope you enjoyed it. No worries, 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 see you on the next MQTT Monday!

Are you interested in learning more about MQTT? Sign up for our newsletter and get notified as soon as a new post is available. If you prefer RSS, subscribe to our RSS feed here.

author HiveMQ Team

About HiveMQ Team

We love writing about MQTT, IoT protocols and architecture in general. Our experts are here to help, so reach out to us if we can help!

mail icon Contact HiveMQ
newer posts MQTT Essentials Wrap-Up
What Is MQTT Keep Alive and Client Take-Over? – MQTT Essentials Part 10 older posts