MQTT Essentials Special: MQTT over WebSockets


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:

  • 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.


  1. I think that the most interesting case is an hybrid environments, in which some clients perform publish/subscribe operations in “pure” MQTT while others do the same but use MQTT over WebSockets. Is this possible with HiveMQ?

    1. Hi Massimo,

      yes, this is absolutely possible. You can define as many plain TCP listeners, SSL listeners, websocket listeners and websocket+SSL listeners as you want. So if you have a hybrid environment, you can connect clients via plain MQTT and other clients via websockets. You can learn more about that here:

      Hope this helps,

  2. Karthikeyan says:

    I need to publish message in Hive MQTT. For that what is frame format i have to publish message. what are all the content i have to add in that frame, like Topic, QoS, Message, Payload, Retained, etc.

    1. Hi Karthikeyan,

      you can use one of the many already existing MQTT client libraries to publish a message to HiveMQ.
      Some of them are listed here (including example code):

      Hope that helps,
      Christoph from the HiveMQ team.

    2. Karthikeyan says:

      I am using AVR microcontroller and SIM900 via TCP with help of serverid and port i am able to connect with MQTT broker. Then what is way to publish message

  3. Haroon Ahmed says:

    I am using MQTT WebSocket client ( Once it is connected to broker the dot color is changed to green and connected is written. But when i disconnect the internet on my device the status is still connected with green color. Is there any solution of this issue ?

    1. Hi,

      the green color will go to red either instantly or when (keepAlive * 1,5) seconds have passed at the latest.

      You can read more about keepAlive here:

      The HiveMQ Team

  4. Valery says:

    I can’t understand – what username and password should i use (and how to create them) when connecting my mqtt client (based on esp8266 module) to

    1. Hi Valery,

      Nice to see you’re taking an interest in MQTT and HiveMQ.

      Our public broker doesn’t require any authentication.

      Kind regards,
      Abdullah from the HiveMQ Team.

Leave a Reply

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