Explainer
Why an edge gateway cannot run MQTT over WebSocket
Short answer
The BASIC script engine on an edge gateway implements MQTT over TCP, optionally over TLS, and sends a raw MQTT CONNECT packet where a wss endpoint expects an HTTP Upgrade to WebSocket first, so that endpoint can never open. Expose a plain MQTT listener on the broker, on 1883 or on 8883 with TLS, or place a broker that dials out over WebSocket between the gateway and the cloud.

On this page
A BASIC script engine on an edge gateway implements MQTT over TCP, optionally wrapped in TLS, and nothing else. A wss:// endpoint is MQTT carried inside WebSocket, and WebSocket begins with an HTTP Upgrade request that the engine never sends, so no script can open that endpoint. The repair is never in the script: expose a plain MQTT listener on the broker, on port 1883 or on 8883 with TLS, or place a broker that speaks WebSocket toward the cloud and plain MQTT toward the gateway in between. Once the transport matches, subscribe and publish behave exactly as in any working example.
The transport the engine speaks #
MQTT is a publish/subscribe protocol that runs over an ordered, bidirectional byte stream; the OASIS specification, versions 3.1.1 and 5.0, requires TCP and treats TLS and WebSocket as optional transports layered on it. The engine's MQTT client follows the plain path: resolve the broker name, open a TCP connection to the configured port, run a TLS handshake when TLS is configured, write an MQTT CONNECT packet, read the CONNACK, then subscribe and publish over that same stream.
Everything the script can set matches that path: a broker address, a port, credentials, and where the engine has it, TLS parameters. There is no URL scheme to select, because the client has exactly one way to reach a broker. That is the whole limitation, and it is a transport limitation, not a broker one.
What a WebSocket endpoint expects first #
ws:// carries MQTT over WebSocket on a plain TCP connection, port 80 by default; wss:// carries the same inside TLS, port 443 by default. RFC 6455 defines the opening: the client sends an HTTP/1.1 GET with an Upgrade: websocket header, the server answers 101 Switching Protocols, and only then does the first MQTT packet flow, each packet inside a WebSocket binary frame. The full exchange, including the key the server must echo back, is walked through in a guide to implementing a WebSocket client with secure wss connections.
Clouds serve MQTT over WebSocket on that port because it is open outbound in almost every corporate firewall, and because a browser dashboard, which cannot open a raw TCP socket, has no other route to MQTT. Reaching a cloud on 443 does not by itself mean WebSocket, though: some endpoints carry plain MQTT over TLS there and select it with ALPN, the case covered in why MQTT over port 443 to a cloud needs ALPN.
Where the connection actually breaks #
Point the engine at a WebSocket listener and the first layers succeed: the TCP connection opens and, on a wss:// endpoint, the TLS handshake completes. The failure comes with the next bytes. The engine writes its MQTT CONNECT packet, whose first byte is the fixed header 0x10, the CONNECT packet type. The listener opposite is an HTTP server waiting for a request line; a binary MQTT packet is not one, so it closes the stream or sits until its timeout expires. Either way no CONNACK returns, and the script reports a failed or timed-out MQTT connection.
That symptom is identical to a blocked port or a wrong port, which is why it gets chased as a firewall or credentials problem. The missing layer sits between TLS and MQTT: the engine has the socket and, where configured, the encryption, and neither implies the HTTP Upgrade step.
What a working example proves #
A published script that publishes to a local broker is real evidence for exactly what it exercised: name resolution, routing, any firewall in the path, credentials, MQTT version and the topic logic, over the transport that example used, MQTT over TCP or over TLS. It is no evidence about WebSocket, because a broker serves each transport on its own listener and the example touched only one.
So qualify any 'works with this broker' claim by transport: the same broker answers MQTT on one port and MQTT over WebSocket on another, and a client that works on the first can be unable to open the second — this gateway is that client. Transport is also not the last hurdle; on some engines the MQTT function set stops at publish, the separate limit described in when an edge gateway has no MQTT subscribe function.
The broker-side fix: a plain MQTT listener #
When the broker is yours — a Mosquitto instance on a server or virtual machine on the plant network or in a DMZ — the gateway never needs WebSocket. Keep the WebSocket listener for browser dashboards and add a second listener for scripts: plain MQTT on port 1883 where the stream stays inside a trusted network, MQTT over TLS on port 8883 wherever it crosses an untrusted link, with the broker's certificate and key configured on that listener. In the broker's configuration each listener carries its own port and the protocol it speaks; both listeners serve the same broker, so a dashboard and the gateway see the same topics.
The plant firewall must allow the gateway's subnet outbound to that port. If the connect to the new listener fails too, the fault is reachability rather than transport, and why an edge gateway cannot connect to your MQTT broker walks that diagnosis.
The plant-side fix: a broker in front of the cloud #
When the only route out is the cloud's WebSocket endpoint, or the firewall allows only the HTTPS port outbound, run a broker on the plant side that accepts plain MQTT or MQTT over TLS from the gateway on a LAN address and holds the upstream connection over WebSocket. Mosquitto fills both roles in one instance: a LAN listener for the gateway and a bridge connection that dials out with a WebSocket URI as its upstream address on current releases. That local-broker-to-cloud pattern is the subject of bridge a local Mosquitto broker to a cloud broker. The bridge host can be a fanless mini PC mounted in the cabinet, and the gateway script changes only its broker address and port.
What the WebSocket leg costs #
Routing the cloud leg over WebSocket costs little, and the numbers say so. RFC 6455 gives every client-to-server frame a 2-byte base header and a 4-byte masking key; payloads of 126 bytes or more add length bytes. A 50-byte PUBLISH in one frame goes on the wire as 2 + 4 + 50 = 56 bytes, an overhead of 6 bytes, or 6 divided by 50 = 12 percent. A 300-byte PUBLISH becomes 2 + 2 + 4 + 300 = 308 bytes, because the 16-bit extended length adds its 2 bytes — an overhead of 8 bytes, or 2.7 percent. The HTTP exchange adds one round trip at connection time: at a 40 ms round trip, about 40 ms once per connection, with keepalive traffic unaffected afterwards.
So overhead is not a reason to keep the gateway off WebSocket; the engine's missing transport is. Spend the effort on the listener or the bridge, not on the wire.
The decision, case by case #
The broker and the firewall decide, not the script:
- Broker under your control and an outbound MQTT port allowed: add the plain listener, 1883 on the trusted LAN or 8883 with TLS across an untrusted link, and point the script at it. This is the default choice.
- Broker under your control but only the HTTPS port allowed outbound: bind that broker's MQTT over TLS listener to the allowed port and skip WebSocket entirely; the engine's TLS support covers it.
- Cloud endpoint that offers only MQTT over WebSocket: run the local broker with the upstream WebSocket bridge and let the gateway speak plain MQTT to its LAN address.
- Cloud endpoint that also serves MQTT over TLS on a dedicated port, as the major IoT hubs do: connect the gateway straight to that port; the WebSocket endpoint beside it exists for browsers, not gateways.
In every case the script stays the working example's script — only the broker address, the port and the TLS settings move.
Questions asked
Can a BASIC script implement the WebSocket handshake itself?
Not through any supported path. The handshake needs an HTTP request with a generated key, the server's protocol-switch reply, and masked frames on every client send. An engine that does not document WebSocket offers no supported way to produce that exchange, so change the broker side instead of hand-rolling framing in script.
How can I tell which transport a broker port carries?
From a laptop with an ordinary MQTT client. A CONNACK after a plain TCP connect means MQTT over TCP; a TLS handshake followed by a CONNACK means MQTT over TLS; a connection that closes or stays silent after the CONNECT packet means the port expects the HTTP Upgrade of WebSocket, or another protocol entirely, and the gateway cannot use it.
Does TLS support on the gateway imply wss support?
No. TLS and WebSocket are independent layers. MQTT over TLS, usually on 8883, only encrypts the stream; wss adds the HTTP Upgrade and the frame layer on top of that encryption. An engine can implement the first and not the second, which is exactly this gateway's position.
Why do cloud brokers offer MQTT over WebSocket on the HTTPS port?
For browsers. A web dashboard cannot open a raw TCP socket, so WebSocket is its only route to MQTT, and the HTTPS port is already open outbound in almost every corporate firewall. Gateways gain nothing from it; where the same cloud also serves MQTT over TLS on a dedicated port, that is the path a gateway takes.

