Table of Contents

Class MqttSessionClient

Namespace
Azure.Iot.Operations.Mqtt.Session
Assembly
Azure.Iot.Operations.Mqtt.dll
public class MqttSessionClient : OrderedAckMqttClient, IMqttClient, IMqttPubSubClient, IAsyncDisposable
Inheritance
MqttSessionClient
Implements
Inherited Members

Constructors

MqttSessionClient(MqttSessionClientOptions?)

Create a MQTT session client where the underlying MQTT client is created for you and the connection is maintained for you.

public MqttSessionClient(MqttSessionClientOptions? sessionClientOptions = null)

Parameters

sessionClientOptions MqttSessionClientOptions

The configurable options for this MQTT session client.

Remarks

When an MQTT session client is constructed with this constructor, it will automatically recover the connection and all previous subscriptions if it detects that the previous connection was lost. It will also enqueue publishes/subscribes/unsubscribes and send them when the connection is alive.

An MQTT session client created with this constructor will only report connection loss and/or publish/subscribe/unsubscribe failures if they are deemed fatal or if the provided retry policy is exhausted. All transient failures will cause the retry policy to be checked, but won't cause the DisconnectedAsync event to fire.

Methods

ConnectAsync(MqttConnectionSettings, CancellationToken)

Connect this client and start a clean MQTT session. Once connected, this client will automatically reconnect as needed and recover the MQTT session.

public override Task<MqttClientConnectResult> ConnectAsync(MqttConnectionSettings settings, CancellationToken cancellationToken = default)

Parameters

settings MqttConnectionSettings

The details about how to connect to the MQTT broker.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<MqttClientConnectResult>

The CONNACK received from the MQTT broker.

Remarks

This operation does not retry by default, but can be configured to retry. To do so, set the RetryOnFirstConnect flag and optionally configure the retry policy via ConnectionRetryPolicy.

Exceptions

InvalidOperationException

If this method is called when the client is already managing the connection.

ConnectAsync(MqttClientOptions, CancellationToken)

Connect this client and start a clean MQTT session. Once connected, this client will automatically reconnect as needed and recover the MQTT session.

public override Task<MqttClientConnectResult> ConnectAsync(MqttClientOptions options, CancellationToken cancellationToken = default)

Parameters

options MqttClientOptions

The details about how to connect to the MQTT broker.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<MqttClientConnectResult>

The CONNACK received from the MQTT broker.

Remarks

This operation does not retry by default, but can be configured to retry. To do so, set the RetryOnFirstConnect flag and optionally configure the retry policy via ConnectionRetryPolicy.

Exceptions

InvalidOperationException

If this method is called when the client is already managing the connection.

DisconnectAsync(MqttClientDisconnectOptions?, CancellationToken)

Disconnect this client and end the MQTT session.

public override Task DisconnectAsync(MqttClientDisconnectOptions? options = null, CancellationToken cancellationToken = default)

Parameters

options MqttClientDisconnectOptions

The optional parameters that can be sent in the DISCONNECT packet to the MQTT broker.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public override ValueTask DisposeAsync()

Returns

ValueTask

A task that represents the asynchronous dispose operation.

PublishAsync(MqttApplicationMessage, CancellationToken)

Publish an MQTT message to the MQTT broker.

public override Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken = default)

Parameters

applicationMessage MqttApplicationMessage

The message to publish.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<MqttClientPublishResult>

The PUBACK received from the MQTT broker.

Remarks

If this operation is interrupted by a connection loss, this client will automatically re-send it once the client has recovered the connection.

This method may be called even when this client is not connected. The request will be sent once the connection is established.

SubscribeAsync(MqttClientSubscribeOptions, CancellationToken)

Send a SUBSCRIBE request to the MQTT broker.

public override Task<MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeOptions options, CancellationToken cancellationToken = default)

Parameters

options MqttClientSubscribeOptions

The details of the subscribe request.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<MqttClientSubscribeResult>

The SUBACK received from the MQTT broker.

Remarks

If this operation is interrupted by a connection loss, this client will automatically re-send it once the client has recovered the connection.

This method may be called even when this client is not connected. The request will be sent once the connection is established.

UnsubscribeAsync(MqttClientUnsubscribeOptions, CancellationToken)

Send a UNSUBSCRIBE request to the MQTT broker.

public override Task<MqttClientUnsubscribeResult> UnsubscribeAsync(MqttClientUnsubscribeOptions options, CancellationToken cancellationToken = default)

Parameters

options MqttClientUnsubscribeOptions

The details of the unsubscribe request.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<MqttClientUnsubscribeResult>

The UNSUBACK received from the MQTT broker.

Remarks

If this operation is interrupted by a connection loss, this client will automatically re-send it once the client has recovered the connection.

This method may be called even when this client is not connected. The request will be sent once the connection is established.

Events

SessionLostAsync

public event Func<MqttClientDisconnectedEventArgs, Task>? SessionLostAsync

Event Type

Func<MqttClientDisconnectedEventArgs, Task>