Search code examples
httpbrowsertcpmqtt

What makes MQTT a raw tcp connection that we can't run it in the browser?


Browsers can't run pure MQTT but we can use websockets to transfer MQTT messages. My question is rather simple. Since MQTT, Http and websockets all rely on TCP, why is it that browsers can talk Http and websockets but not MQTT. Browsers can't open a raw TCP connection but what exactly makes MQTT open a raw TCP connection?


Solution

  • All of HTTP, MQTT, Websockets use TCP directly (HTTP/3 uses UDP), which what you describe as a "raw" connection. But these are different application level protocols on top of TCP. For security reasons a browser does not allow "raw" TCP connections from within an web application. Instead it restricts what application protocol can be used. Anything else must be somehow translated into an allowed application protocol, for example tunneled over WebSockets instead of using "raw" TCP directly.

    ... but we can use websockets to transfer MQTT messages

    Only if there is a backend which can translate the MQTT encapsulated in WebSockets into plain MQTT, i.e. remove the outer WebSockets tunnel.