What are merits of MQTT over WebSocket compared to direct MQTT?
I'm considering using MQTT in my project and so I want to know why some people choose MQTT over WebSocket instead of direct MQTT.
WebSockets are specially designed for point to point connections between a client and a server. However, MQTT adds on extra abstraction on top of the basic message sending mechanism such that multiple interested machines can subscribe to the topic of their interest.
MQTT over Websockets allows you to receive MQTT data directly into a web browser. This is important as the web browser may become the DE-facto interface for displaying MQTT data. MQTT websocket support for web browsers is provided by the JavaScript client.
The real advantage of MQTT over HTTP occurs when we reuse the single connection for sending multiple messages in which the average response per message converges to around 40 ms and the data amount per message converges to around 400 bytes. Note that in the case of HTTP, these reductions simply aren't possible.
You should only need to run MQTT over websockets if you intend to publish/subscribe to messages directly from within webapps (in page).
Basically I would run pure MQTT for everything and only add the websockets if you actually need it.
For all the non-browser languages the MQTT client libraries only use native MQTT. For Javascript there is both a pure MQTT library and the Paho in page library that uses websockets.
Edit: The firewall tunnelling use case is a valid reason to use MQTT over websockets and since writing this answer more of the none web/JavaScript client libraries have added support
Two main reasons for using MQTT over Websockets (which effectively means going over HTTP/HTTPS):
If you don't need or worry about the above, use "direct" MQTT:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With