26

What are the major differences between MQTT and Web Sockets?

When using IoT for home automation - control and monitoring access over different devices, which one of them should be used when Rest API based and browser based accessibility is required.

I am using Java (Pi4J Library) on a Raspberry Pi 2 B+.

I have a setup of several sensors like light and dark, humidity, PID etc.

I also have a cloud server where I can send the data if required.

Aurora0001
  • 18,520
  • 13
  • 55
  • 169
Shakti Phartiyal
  • 713
  • 1
  • 6
  • 15

2 Answers2

28

The question setting here is little bit misleading, because actually these protocols cannot at all be compared together. They are like TCP and IP, layers above each other. [1]

Websockets is a low level protocol to provide things that its 'competitor' RESTful http that is on same level does not provide: an always open channel without need for open and close on every request. [2]

MQTT provides a light weight way to publish or subscribe data. The confusion may be that those subscription are some sort of channels, but that is different type of channel. To make a constant open connection in MQTT you need Websockets AND MQTT at same time.

In IoT, as well in any design, you have to select if you need a stream or not (WebSockets vs RESTful) and about MQTT you may have to think whether you want a subscription and publishing mechanism on your app.

On some circumstances you may consider MQTT over WebSockets, if any common thing is around. [3]

Answer to the question:

You say you have a setup of a Rasperry Pi and several sensors around the place. If the sensors are far from Rasperry with their own controllers, you can use MQTT to collect the data. To store data to cloud, send the data in HTTP. In the cloud provide data through rest. [4]

For websockets there is no need, but if you find it useful, do use it.

Sources:

[1] https://www.quora.com/What-are-the-pros-and-cons-of-WebSockets-versus-MQTT-as-real-time-web-infrastructure-for-the-Internet-of-Things

[2] https://www.pubnub.com/blog/2015-01-05-websockets-vs-rest-api-understanding-the-difference/

[3] https://stackoverflow.com/questions/30624897/direct-mqtt-vs-mqtt-over-websocket

[4] http://www.theinternetofthings.eu/antonio-grasso-mqtt-vs-http-what-best-protocol-iot

mico
  • 4,351
  • 1
  • 18
  • 27
13

They're comparable in that both allow you to have full-duplex communication such that the server can immediately pass data to the client, without the client polling for it (as might be with HTTP).

However, Websockets is designed for a simple point-to-point connection between a client and a server. MQTT layers extra abstractions on top of basic message sending, so that multiple interested parties can subscribe to messages that may interest them. Messages can therefore be routed by 'message topic' so that many clients can share a notional queue, where a server can choose to hear all messages from all clients, but may also filter by topic.

MQTT has a variety of other useful features, e.g. retained messages, such that subscribers immediately receive the message, and the LWT (Last Will and Testament) which is a message that can be sent automatically if the client abnormally disconnects. In summary, MQTT is 'higher up the stack' offering features and abstractions that a simple Websocket does not.

TheMagicCow
  • 596
  • 2
  • 4