ChatGPT解决这个技术问题 Extra ChatGPT

Direct MQTT vs MQTT over WebSocket [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago. Improve this question

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.


B
Brian Burns

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


The corporate firewall case that user5762813 and Vasif outlines is actually very important in many settings. I want my devices to be able to connect to a broker, even if they only can reach the internet by going through an HTTP proxy.
u
user5762813

Two main reasons for using MQTT over Websockets (which effectively means going over HTTP/HTTPS):

Web apps (those running in a browser - e.g. written in JavaScript)

Any other applications that don't want to use the 1883/8883 port and want to go over HTTP/HTTPS instead - this could be so that there is less of a chance of being blocked by a firewall (e.g. in a corporate network), as most firewalls will let HTTP traffic through

If you don't need or worry about the above, use "direct" MQTT:

it is more efficient

there are more client libraries for various languages that work with "direct" MQTT


Saying "which effectively means going over HTTP/HTTPS" isn't quite right. Websockets are advantageous because they fundamentally aren't using HTTP/HTTPS and the implied overhead. Have a read through (HTML5 WebSocket: A Quantum Leap in Scalability for the Web)[websocket.org/quantum.html] for all the details.
B
Brian Burns

MQTT is a protocol which supports following:

Provides publish/subscribe mechanism

Quality of Service policy

Have minimal overhead in communication

Specifically designed for narrowband communication channel and constrained devices.

Depending upon the device there is an implementation available.

Browser : It uses websockets. Websocket provides browsers with a capability to establish a full duplex communication. There is Javascript library to implement MQTT functionality, see Eclipse Paho JavaScript Client

Android : Their is a MQTT client library written in Java for developing applications on Android. See Eclipse Paho Android Service

So it depends on device that is going to use this functionality. For standards and specifications please visit MQTT Version 5.0

Hoping this helps.

Cheers !


R
Rin Minase

MQTT over websockets is perfect if ever a certain webpage is the sending or the receiving MQTT client.

A good summary of the capabilities of MQTT over websockets can be found here.


V
Vasif

MQTT over web sockets is also helpful if the application is running behind a firewall that allows only 443 and 80 traffic. And, you have no control over the policies of the firewall.


There is nothing about MQTT that prevents you from using 443 or 80 or what have you if you are using TLS/DTLS(encryption). I guess a firewall could theoretically block non HTTP traffic on port 80 but it wouldn't know that it is HTTP if you use TLS, and there are very few cases were you wouldn't want to use TLS to secure a connected device
k
kiran malvi

MQTT Broker:

The counterpart of the MQTT client is the MQTT broker. The broker is at the heart of any publish/subscribe protocol. Depending on the implementation, a broker can handle up to thousands of concurrently connected MQTT clients.

MQTT Client: When we talk about a client, we almost always mean an MQTT client. Both publishers and subscribers are MQTT clients. The publisher and subscriber labels refer to whether the client is currently publishing messages or subscribing to messages (publish and subscribe functionality can also be implemented in the same MQTT client).

WebSocket: We have learned in the MQTT Essentials that MQTT is ideal for constrained devices and unreliable networks. It’s also perfect for sending messages with a very low overhead. It would be quite nice to send and receive MQTT messages directly in the browser of a mobile phone or in general. This is possible by MQTT over WebSockets.

You can use a third-party protocol. PAHO, EMQTT, VerneMQ.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now