I am new to this IoT protocol area. My understanding is that MQTT is a lightweight messaging protocol for IOT devices.

MQTT over web socket involves use of HTTP to UPGRADE the connection to use web socket. Otherwise they follow same protocol for data exchange.

Both sit on top of the TCP layer.

Both support a persistent connection.

Both support pub/sub model.

The use case difference between the 2 is said to be that MQTT over web socket is ideal when client is a browser since it is difficult to implement MQTT in browser (but this can be made possible by using Socket API).

So what exactly is the technical difference between MQTT and MQTT over web socket that allows the later to be preferred by web browser apps?

  • 1
    The only "Socket API" available in browsers is the web socket API. So MQTT over web sockets is not "preferred" in browsers, it's the only option.
    – jcaron
    Oct 8, 2020 at 12:26
  • Thanks for the clarification. This article "hivemq.com/blog/mqtt-essentials-special-mqtt-over-websockets" suggest that - 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.
    – variable
    Oct 12, 2020 at 6:05
  • There have been a few attempts to define a lower-level socket API, but as fas as I know, none went anywhere, and I don't know of any browser implementing any of those, and caniuse and MDN do not seem to know anything about it either. The article you linked to dates from 2015 when the TCP and UDP Socket API was still in discussion, but it has been abandoned that same year.
    – jcaron
    Oct 12, 2020 at 9:13

1 Answer 1


Nothing, as I stated in the answer on Stack Overflow they are byte for byte exactly the same protocol, it's just the transport layer which changes from raw TCP to Websockets.

The difference is that you CAN NOT open raw sockets from within the browser, the security sandbox will not allow you to open arbitrary TCP connections to random hosts. The sandbox will only allow HTTP based protocols, which includes WebSockets because they bootstrap from an initial HTTP request.

The other benefit of MQTT over Websockets is that it can make use of existing proxy infrastructure if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.