Wednesday, September 10, 2014

MQTT over WebSocket

MQTT over WebSocket sounds like "tårta på tårta" in the first place, like the Swedes tends to say. But is it really like that, just plain overhead?

All major MQTT brokers do support MQTT over WebSocket nowadays, including Mosquitto, ActiveMQ, HiveMQ, and more. What is it good for? I have already discussed about differences of these two protocols in my earlier posting MQTT vs. WebSocket.

There are at least two scenarios where MQTT over WebSocket does make sense:

a) The first and rather obvious one is use of MQTT directly from script running inside Web browser. WebSocket is called as the TCP socket of the Web. And it is typically the only supported way of exchanging real-time data, in addition to http polling.So, what ever protocol you want to use in your dynamic web page with client side content rendering, tunneling through WebSocket is the only way to go.

Tunneling always causes some extra overhead in data communication, but it can do some good as well. In many organizations, firewall rules may block direct MQTT communication, but WebSocket traffic is usually happily accepted.  So it may be your only way to get through the firewall, even if you're doing no evil.

b) Not only firewalls, but there may be other technical reasons preventing usage of the protocol of your choice. Many cloud environments which are build a top of CloudFoundry standard only accept incoming connections if forms of Http(s) and WebSocket(s). In order to use plain MQTT only, an external server running the broker is needed, and the connection is then created from the Cloud to the server. Many clouds do allow doing so.

But that's a bit against the whole philosophy of cloud services. Better to have the MQTT broker running in the cloud, and let connections to be created from outside world over the WebSocket.

MQTT tunneling over WebSocket
Two or more MQTT brokers may be connected over WebSocket. In a sensor network kind of schenario, it may make sense that not every sensor connects directly to the back-end system (cloud). Instead let there be number of intermediate brokers to reduce the number of direct connections the back-end system must handle.

If sensors are installed in a plant or other controlled environment, they may use unsecured mean of communication to the nearest broker, and then only the inter-broker connection needs to be secured. The broker may also have some local data storage to buffer the data in case of connection interruptions, thus no data is lost permanently.

* "tårta på tårta", direct translation is "cake on cake". Depending on context, it may be interpreted as tautology or recursion.

No comments:

Post a Comment