MQTT Vs WebSocket

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

MQTT vs.

WebSocket - Key differences and when to


use them together
ably.com/topic/mqtt-vs-websocket

MQTT (Message Queuing Telemetry Transport) and WebSocket are communication


protocols often used to facilitate realtime communication between IoT applications, web
browsers, and devices. Often, people find it challenging to select the right one for their
specific use case.

Although MQTT and WebSocket are somewhat in competition (in the sense that you can
use either one of them for some use cases, such as live chat for mobile devices), they also
complement each other; you can use MQTT over WebSockets to consume MQTT
messages in a browser.

In this blog post, we will compare these two protocols, help you understand how they
work, how they complement each other, and the distinctive characteristics of MQTT and
WebSocket.

Below are the topics that we’ll discuss in this post:

What is WebSocket?

What is MQTT?

Using WebSocket with MQTT

Comparing MQTT and WebSocket

MQTT vs. WebSockets - which is better?

What is WebSocket?
WebSocket is a realtime protocol that provides a persistent, full-duplex communication
channel between a web client and a web server over a single TCP connection. WebSocket
is ideal for systems requiring realtime updates and in scenarios where client and server
communication is bidirectional and doesn't belong to the simple "request-response"
schema. Examples include financial tickers, realtime multiplayer games, chat apps, and
realtime geolocation updates.

1/8
2/8
A WebSocket connection starts as an HTTP request/response handshake between the
client and the server. The client always initiates the handshake; it sends a GET request to
the server, indicating that it wants to upgrade the connection from HTTP to WebSockets.
The server must return an HTTP 101 Switching Protocols response code for the
WebSocket connection to be established.

Once the connection upgrade is successful and switches from HTTP to WebSocket, the
client and server can freely exchange messages over the connection as and when needed.
After the WebSocket connection has served its purpose, it can be terminated via a closing
handshake (both the client and server can initiate it).

What is MQTT?
MQTT is a lightweight communication protocol that uses the publish/subscribe
messaging pattern to enable machine-to-machine communication on networks with
limited bandwidth or unpredictable connectivity. It's simple to use, scalable and ideal for
small-sized, constrained devices with limited CPU, memory, and battery life.

3/8
Similar to WebSockets, MQTT runs over TCP. Unlike WebSockets, MQTT natively
provides one-to-one, one-to-many, and many-to-many data streams between devices and
applications. In an MQTT architecture, we have:

Publishers (producers) and subscribers (consumers).

A broker which acts as the middleware MQTT server that manages the exchange of
messages between publishers and subscribers. Note that messages are stored in
topics (or channels).

Using WebSockets with MQTT


In the previous section, we established that MQTT is suitable for high-latency networks
and constrained devices such as low-powered devices and IoT sensors. One thing worth
mentioning is that you can't directly send MQTT messages to a browser, because web
browsers don’t have MQTT support built-in.

4/8
However, you can send MQTT messages to a browser client over WebSockets. Many
MQTT client libraries such as Paho, MQTT X Web, and MQTT.js support WebSockets.
These libraries provide the essential capabilities browser clients need to establish
connections to MQTT brokers for subscribing to topics, publishing, and receiving
messages.

To give a practical example, you can use MQTT over WebSocket to collect data from IoT
sensors (e.g., gas sensors); this is done over MQTT. Data from sensors is transferred to
the MQTT broker, which can then push it to a browser client over WebSockets (for
example, to power a live dashboard that monitors gas levels and warns against imminent
hazards).

Of course, as both MQTT and WebSockets are bidirectional, you can also send messages
from web browsers (over WebSockets) to the MQTT broker, which can then forward them
to the IoT consumers.

Comparing MQTT and WebSocket


Now that you know what MQTT and WebSockets are and what it means to use MQTT
over WebSockets, the following sections will help you better understand the different
characteristics of MQTT and WebSocket, the performance of MQTT over TCP vs MQTT
over WebSocket, and how MQTT and WebSocket compare with HTTP.

MQTT vs. WebSocket characteristics

5/8
The table below compares the major differences between MQTT and WebSocket.

Criteria MQTT WebSocket

Use cases MQTT is suitable for low- WebSockets are suitable for web apps
powered devices with limited and devices unrestricted (or less
CPU and memory, and restricted) by low bandwidth. Suitable for
unreliable, low-bandwidth use cases such as as financial tickers,
networks. It’s the go-to online multiplayer games, chat,
protocol for IoT use cases. geolocation updates, live polls, etc.

Message One-to-many One-to-one


distribution

Architecture MQTT uses a Bidirectional, socket-like API. Note: many


publish/subscribe WebSocket frameworks and libraries add
messaging pattern. pub/sub capabilities on top of
WebSockets.

Messaging MQTT has three QoS Raw WebSockets don't provide QoS
QoS (quality of service) options: 0 options.
(at most once delivery), 1 (at
least once delivery), and 2
(exactly-once delivery).

Underlying TCP TCP


protocol

Messaging Asynchronous, event-based Asynchronous, event-based


mode

Message Maximum of 256 MB 2^63 bytes per frame


size

Secure TLS TLS


connections

Message Minimum of 2 bytes Minimum of 2 bytes for all messages


overhead except masked frames, which have a
minimum of 6 bytes.

Message MQTT brokers queue Raw WebSocket doesn't support


queuing messages for all message queuing.
disconnected clients
subscribed to a topic.

MQTT over TCP vs. MQTT over WebSocket performance

The table below compares the performance of MQTT over TCP and MQTT over
WebSockets. Unsurprisingly, the performance of MQTT over TCP is faster than MQTT
over Websocket, and this is primarily due to the overhead in MQTT over Websocket
architecture.

6/8
MQTT over TCP has relatively minimal overhead. In comparison, the overhead with
MQTT over Websocket is slightly higher. This is caused by the initial exchange of complex
headers between client and server and the piping and unpacking at both the client and
server end.

MQTT Over TCP (in MQTT Over WebSocket (in


Criteria Bytes) Bytes)

Subscription request 291 299

Establish connection 512 1161

Disconnect 334 418

QoS 1 message published / 545 555


256 byte

MQTT over TCP vs MQTT over WebSocket. Source: https://go.ably.com/b73

How do MQTT and WebSocket compare with HTTP?

One factor differentiating MQTT and WebSocket from HTTP (Hypertext Transfer
Protocol) is that the MQTT and WebSocket are event-driven protocols, while HTTP is
request-driven.

WebSockets are stateful, and the connection between a client and a server remains active
until either the client or server closes the connection. MQTT is also stateful and operates
differently than HTTP - it uses push-based pub/sub communication instead of the
GET/POST pattern in HTTP.

In contrast to WebSocket and MQTT, HTTP is stateless and needs a new connection with
every request (this process is very resource-intensive). HTTP is sometimes referred to as
an asymmetric 'request-and-response' client-server protocol. Learn more about the
differences between WebSocket and HTTP.

Conclusion: MQTT vs WebSockets - which is better?


The choice of which protocol to use entirely depends on your use case and what you're
trying to achieve. If you aim to enable M2M communication for sensors, IoT applications,
and apps operating in networks with low bandwidth and high latency, then it's advisable
to use MQTT.

If you want to enable full-duplex, two-way communication in an environment where you


don't necessarily deal with constrained devices, then you should consider using
WebSockets. As mentioned before, you can use WebSockets to build functionality like
chat applications, realtime multiplayer games, and live geolocation updates, among many
other examples. Consider using MQTT over WebSocket if you want to send MQTT
messages directly from low-powered devices and sensors to web browsers.

7/8
Ably is a realtime platform designed for last-mile pub/sub messaging. We make it easy for
developers to build realtime experiences like chat, live dashboards, and asset tracking for
millions of users, without the hassle of managing and scaling infrastructure.

At Ably, we embrace open standards and interoperability. Our realtime client library is
WebSocket-based. Going beyond WebSockets, we believe that you should have the
flexibility to choose the right protocol for the job at any given moment. That’s why in
addition to WebSockets we support many other protocols, including MQTT.

Sign up for a free account and explore our documentation to discover how you can
build scalable multi-protocol apps with Ably.

8/8

You might also like