Backend Development 17 min read

EMQX Project Overview and Detailed Code Walkthrough of Connection and Session Layers

This article introduces the open‑source EMQX MQTT broker, explains the MQTT protocol basics, and provides an in‑depth analysis of the broker's connection and session processes—including their state structures, message flows for connect, subscribe, unsubscribe, ping, disconnect, and QoS 0/1/2 publishing—supported by illustrative code snippets and diagrams.

360 Smart Cloud
360 Smart Cloud
360 Smart Cloud
EMQX Project Overview and Detailed Code Walkthrough of Connection and Session Layers

EMQX (Erlang/Enterprise/Elastic MQTT Broker) is an open‑source MQTT server built on the Erlang/OTP platform, offering soft‑realtime, low‑latency, and distributed capabilities for IoT messaging.

The MQTT protocol is a lightweight publish‑subscribe protocol designed for low‑bandwidth, unreliable networks, and the article briefly outlines its control packet types.

A code‑centric exploration focuses on the connection layer ( emqx_connection.erl , emqx_protocol.erl ) and the session layer ( emqx_session.erl , emqx_broker.erl ). Each client creates a connection process (ConnPid) and a session process (SPid) that interact via messages.

Connection Process : Handles packet parsing, authentication, and state management. Example state excerpt:

{status,<0.2190.0>,
{module,gen_server},
[[{incoming_bytes,166},
{{subscribe,<<"mqttbroker/xxx">>},{allow,1570872922716}},
{'$ancestors',[<0.1876.0>,<0.1875.0>,esockd_sup,<0.1555.0>]},
{force_shutdown_policy,#{max_heap_size => 0,message_queue_len => 0}},
...,
#state{transport = esockd_transport,
       socket = #Port<0.29>,
       peername = {{127,0,0,1},55952},
       conn_state = running,
       proto_state = #pstate{zone = external, sendfun = #Fun
},
       client_id = <<"mqttbroker/slw">>,
       session = <0.2192.0>,
       keepalive = 60,
       connected = true}}

The connection process receives upstream packets, invokes emqx_protocol:process_packet/2 , and sends responses such as CONNACK, SUBACK, PUBACK, PINGRESP, and DISCONNECT.

Session Process : Manages MQTT business logic, including subscription handling, inflight window management, and QoS guarantees. It receives cast or call messages from the connection process (e.g., {subscribe, Self, SubReq} , {puback, PacketId, ReasonCode} ) and dispatches messages to other sessions.

Message Flow Highlights :

Connect: Client sends CONNECT, broker creates a session, binds ConnPid and SPid, and replies with CONNACK.

Subscribe/Unsubscribe: ConnPid forwards the request to the session via emqx_session:subscribe/4 ; the session replies with SUBACK.

Ping: ConnPid immediately returns PINGRESP.

Disconnect: Client sends DISCONNECT, ConnPid stops gracefully; the session exits on EXIT notification.

Publish (QoS 0): ConnPid calls emqx_session:publish/3 , which directly delivers the message to the target session and then to the target connection.

Publish (QoS 1): In addition to delivery, an inflight entry is created; the broker expects PUBACK from the client and clears the inflight entry on receipt.

Publish (QoS 2): A three‑step handshake (PUBREC, PUBREL, PUBCOMP) is coordinated via synchronous calls between ConnPid and the session, with awaiting‑rel and inflight structures ensuring ordered, reliable delivery.

The article concludes that the device‑to‑Conn and Conn‑to‑Session interactions are now clearly understood, laying a foundation for further exploration of routing and message distribution logic.

BackendIoTprotocolMQTTMessageBrokerErlangEMQX
360 Smart Cloud
Written by

360 Smart Cloud

Official service account of 360 Smart Cloud, dedicated to building a high-quality, secure, highly available, convenient, and stable one‑stop cloud service platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.