MQTT GUIDE
3 min readMQTT over WebSocket
MQTT over WebSocket carries the same MQTT protocol inside a WebSocket connection, so browsers and other HTTP-only clients can publish and subscribe directly. Brokers commonly expose it on port 8084 (WS) or 8085 (WSS).
MQTT was built for raw TCP sockets, which browsers can't open. MQTT over WebSocket bridges that gap: it wraps the exact same MQTT packets in a WebSocket connection, so a web page becomes a first-class MQTT client with no gateway or polling in between.
How it works
A WebSocket connection starts life as an ordinary HTTP request with an Upgrade: websocket header. Once the
broker responds with 101 Switching Protocols, the connection flips to a persistent, bidirectional
channel. From that point the client sends the identical MQTT packets — CONNECT, SUBSCRIBE,
PUBLISH — as WebSocket binary frames, and the broker pushes messages back the same way. The MQTT layer doesn't
change; only the transport underneath it does.
MQTT transports at a glance
| Transport | Typical port | Who uses it |
|---|---|---|
| MQTT over TCP | 1883 | Devices, backend services |
| MQTT over TLS (MQTTS) | 8883 | Devices needing encryption |
| MQTT over WebSocket (WS) | 8084 | Browsers, HTTP-only clients |
| MQTT over WebSocket Secure (WSS) | 8085 | Browsers needing encryption |
WebSocket support is what makes live browser dashboards and controls possible — the page subscribes to a topic and updates the instant a message arrives, with no server-side relay.
MQTT over WebSocket in TBMQ
TBMQ exposes WebSocket on port 8084 and secure WSS on 8085 (the WS listener
is enabled by default; enable WSS to add TLS). It also ships a built-in
WebSocket MQTT client right in its UI, so you can publish and subscribe
from the browser without writing any code — a fast way to test topics and see live traffic. For setup and connection details,
see the
MQTT over WebSocket guide.
Frequently asked questions
What is MQTT over WebSocket?
It is the standard MQTT protocol carried inside a WebSocket connection instead of a raw TCP socket. The MQTT packets are identical — WebSocket is just the transport — so a browser can be a full MQTT client, publishing and subscribing in real time.
Why do browsers need MQTT over WebSocket?
Browsers cannot open raw TCP sockets for security reasons, so they cannot speak plain MQTT on port 1883. WebSocket is the browser-native, firewall-friendly transport that lets web apps talk to an MQTT broker directly.
What ports does MQTT over WebSocket use?
It varies by broker. TBMQ uses port 8084 for WebSocket (WS) and 8085 for secure WebSocket (WSS). Note that 8083 is TBMQ’s HTTP UI and REST port, not the WebSocket port.
Is MQTT over WebSocket slower than plain MQTT?
There is a small amount of WebSocket framing overhead per message, but it is minor. The connection is still long-lived and the broker still pushes messages instantly, so for browser clients the practical performance is excellent.
Can I secure MQTT over WebSocket?
Yes, with WSS — WebSocket over TLS — which encrypts the connection the same way MQTTS does. Note that browsers can’t present client certificates for mutual TLS over WSS; non-browser clients (Node, Python, Java) can.
Run it yourself
TBMQ is a free, open-source MQTT broker built to scale. Spin it up in minutes or try the live demo — no install required.