12

According to this blog, Mosquitto (the MQTT broker) now supports connecting to clients over web sockets. The blog article seems to hint that web sockets are more useful for browser applications, since web browsers don't support proper TCP sockets (yet), although the web socket protocol is supported by the majority of modern browsers.

If I just have various clients in a network (e.g. sensors and actuators based on microcontrollers such as Raspberry Pis), will there be any advantage to using web sockets over direct TCP connections? Is the overhead of the web socket protocol only worth it when you are communicating with a browser?

Aurora0001
  • 18,520
  • 13
  • 55
  • 169

3 Answers3

9

The question here appears to be "should I use MQTT over TCP, or use MQTT over websockets (which also goes over TCP)?" In other words, is "encapsulating MQTT in the websockets protocol a good idea?"

This is (almost) entirely down to your application and whether you need websockets support - probably for consuming messages in a browser or for firewall reasons. If you can't have your server be accessible on port 1883 or better 8883 for pure MQTT, then websockets may be your best option.

Websockets does require extra bandwidth, but whether that is important to you is something only you can answer.

It's also worth noting that in current versions of Mosquitto, websockets don't work as well as they could so there can be extra latency when sending/receiving websockets messages. That is something that will not be an issue in future versions though.

ralight
  • 880
  • 4
  • 5
7

When you are communicating only inside your network (intranet), using pure TCP will be fine. But if you have to connect to another server, problems will arise.

Because most of the modern servers do not allow clients to connect through random ports. They only allow some dedicated ports to connect. That's all. Hence if you have to connect to another server, better to use websocket rather than pure TCP connection.

If you are considering the overhead, it's not That much bigger. You can refer this article, if you want to know more about the websocket's overhead.

In my personal opinion, it's better to use websocket always, except you have some serious concerns.

ThisaruG
  • 824
  • 6
  • 16
6

tl;dr - always prefer free libraries to coding it your self (unless you have extreme requirements)


Should I use Mosquitto's web sockets or connect clients directly?

How long is a piece of string? (YMMV)

I can only speak generally, but I always prefer wrapper libraries to raw sockets (or, indeed, to coding anything which I can get for free from a library).

They make coding simpler and less error prone. They take care of a lot of house-keeping and error handling, which is code that you would have to write and debug yourself, where as a library has generally been well reviewed and tested and is being used by thousands of others, all of whom will report/fix bugs for you.

Plus, it is less code for you to maintain (and, possibly, port), which means more time to develop, test & polish your app, or move on to the next one.

The only overhead is arguably a function call, if you accept that all that librarian goodness (error handling, hose-keeping & the like) is something that you would have to code yourself in order to get good, stable, software.

If you are concerned about performance, just profile. But, unless your socket is active hundreds of times a second, I would not even bother.

Mawg
  • 3,147
  • 1
  • 13
  • 35