1. Basic Concepts

The Session Session

define

  • Definition: logical level communication between a client (identified by ClientID) and a server
  • Life cycle (lifetime) : Session >= network connection

The CleanSession flag at Connect is set to 0 by the client — enabling session reuse. After the network is disconnected and reconnected, the Session information is restored. The client and server need to have relevant Session persistence mechanism. 1 — Turn off session reuse. Each Connect is a new Session, and the Session only lasts as long as the network connection.

The client Session has been sent to the server, but QoS 1 and QoS 2 messages have not been confirmed. QoS 2 messages have not been confirmed

Server Session Whether the Session exists, even if the rest of the Session state is empty (SessionFlag) The subscription information from the client has been sent to the client, QoS 1 and QoS 2 messages that have not been confirmed are to be sent to the client. QoS 1 and QoS 2 messages that have not been confirmed are received from the client. (Optional) QoS 2 messages that have not been confirmed are to be sent to the client

Second, q&A on the agreement

Q: To ask an MQTT protocol question, why cleansession distinguishes 0 from 1, and what are their respective uses and scenarios?

A: When the value is 1, the server must re-establish each session, which is also the case in most scenarios.

A value of 0 can reuse historical sessions. Server sessions are stateful and can record a lot of information.

If the value is 0 then you can hold sessions, and this hold session is useful, if the server implements it, for things like unsent history messages and so on, authentication information and so on, then you can use it. You can compare it to the session that we used to log in to the web page, and if it doesn’t expire, you don’t need to log in.

To receive offline messages, it is necessary to use cleansession=0.

No matter what the clean Session value is, when the terminal is offline, messages with QoS=0,1, and 2 cannot be received. When the clean Session value is set to 1, the terminal cannot receive messages with QoS=0,1, and 2 when the terminal is offline and online again. When the clean Session value is set to 0, when the terminal goes offline and comes online again, messages with QoS=0,1, and 2 can still be received. If the same subject sent many receive many, one is not bad, according to the order to receive all.

Emq source code, hook up and down process

Emq v2 When the server process is restarted, the session is cleared and local persistence is not implemented.

When cleansession is 0 and 1, the up and down lines in hook are very different. Same Clientid conflict login.

Emqttd :hook(‘client.connected’, fun? MODULE:on_client_connected/3, [Env]), emqttd:hook(‘client.disconnected’, fun ? MODULE:on_client_disconnected/3, [Env]), emqttd:hook(‘session.created’, fun ? MODULE:on_session_created/3, [Env]), emqttd:hook(‘session.terminated’, fun ? MODULE:on_session_terminated/4, [Env]),

(1) Cleansession ==1 The client own online offline Inactive session (firecat_heartbeat001 / firecatGTerm) created. Client firecat_heartbeat001 connected, connack: 0 normal offline client firecat_heartbeat001 disconnected, reason: normal session (firecat_heartbeat001 / firecatGTerm) terminated: normal.

In scenario 2, the client is online normally, and then the same Clientid client is online again elsewhere. Conflicts clientid normal online first session (firecat_heartbeat001 / firecatGTerm) created. Client firecat_heartbeat001 connected, connack: 0 post-conflict online session (firecat_heartbeat001 / firecatGTerm) terminated: {shutdown,conflict}. session(firecat_heartbeat001/firecatGTerm) created. client firecat_heartbeat001 connected, Connack: 0 Note that when the conflict goes online, the latter will crowd out the former, but the former will not trigger the offline function, just destroy the session and rebuild. The latter normal offline client firecat_heartbeat001 disconnected, reason: normal session (firecat_heartbeat001 / firecatGTerm) terminated: normal.

(2) Cleansession ==0 The client’s own online referral inactive session for the first time (firecat_heartbeat001 / firecatGTerm) created. Client firecat_heartbeat001 connected, connack: Client firecat_heartbeat001 disconnected, reason: normal See emqttd_session. Erl session (firecat_heartbeat001 / firecatGTerm) terminated: {shutdown, expired}.

Scenario 2: The session client firecat_Heartbeat001 connected, connack does not create the session client firecat_Heartbeat001 connected, connack: 0 Firecat_heartbeat001 disconnected client, reason: Normal notice we don’t destroy the session and eventually, the session will expire, the time will be destroyed, Time to see the emqttd_session. Erl session (firecat_heartbeat001 / firecatGTerm) terminated: {shutdown, expired}.

In scenario 3, the client is first online and then the same Clientid client is online again elsewhere. Conflicts clientid normal online first session (firecat_heartbeat001 / firecatGTerm) created. Client firecat_heartbeat001 connected, connack: Client firecat_heartbeat001 connected, connack: 0 Firecat_heartbeat001 connected, connack: 0 Firecat_heartbeat001 disconnected client, reason: Normal notice we don’t destroy the session and eventually, the session will expire, the time will be destroyed, Time to see the emqttd_session. Erl session (firecat_heartbeat001 / firecatGTerm) terminated: {shutdown, expired}.

 

Related reading:

MQTT protocol in general

Key words: MQTT protocol emQTTD project Clean Session and Retained Message

My MQTT protocol and EMQTTD Open Source project personal understanding (4) – client CleanSession=0 when on-line receiving offline messages, source code analysis