Static class to help manage sessions.


  • CreateNullSession(string host)

Creates and returns with a Null session. When a null session is used the client let the broker assign a client id to the client.

  • IEnumerable GetSessions(string host)

Returns with all the sessions for the given host.

  • bool HasAny(string host)

Returns true if there’s at least one stored session for the given host.

  • Session Get(string host, string clientId = null)

Loads the session with the matching clientId or creates a new one with it. If clientId is null either the last used session is used or if there’s no previous session found it creates one with a random ID.

  • void Delete(string host, Session session)

Removes the session from the session list of the given host.


This example creates

// Create the client
var client = new ConnectionOptionsBuilder()
        .WithTCP("localhost", 1883)
// Set an OnConnected callback to log out the broker assigned client ID
client.OnConnected += OnConnected;
// Begin the connection process to the broker and pass a callback function to create/modify a ConnectPacketBuilder

private ConnectPacketBuilder ConnectPacketBuilderCallback(MQTTClient client, ConnectPacketBuilder builder)
    // Get the broker's host from the MQTTClient
    var host = client.Options.Host;

    // If no previous session is created assign a Null session to the builder
    if (!SessionHelper.HasAny(host))
        Debug.Log("Creating null session!");
        builder = builder.WithSession(SessionHelper.CreateNullSession(host));
        Debug.Log("A session already present for this host.");

    return builder;

private void OnConnected(MQTTClient client)
    // Get the session and print out the broker assigned client ID
	var session = SessionHelper.Get(client.Options.Host);

    // ...