Table of Contents

Interface IMqttPubSubClient

Namespace
Azure.Iot.Operations.Protocol
Assembly
Azure.Iot.Operations.Protocol.dll

An MQTT client interface that is intentionally limited to publish and subscribe operations. Implementations of this interface may provide connect/disconnect functions and may or may not include retry logic to handle when a publish or subscribe is attempted when disconnected.

public interface IMqttPubSubClient : IAsyncDisposable
Inherited Members

Properties

ClientId

Returns the client Id used by this client.

string? ClientId { get; }

Property Value

string

Remarks

If a client Id has not been assigned yet by the user or by the broker, this value is null.

ProtocolVersion

The version of the MQTT protocol that this client is using.

MqttProtocolVersion ProtocolVersion { get; }

Property Value

MqttProtocolVersion

Methods

DisposeAsync(bool)

ValueTask DisposeAsync(bool disposing)

Parameters

disposing bool

Returns

ValueTask

PublishAsync(MqttApplicationMessage, CancellationToken)

Publish a message to the MQTT broker.

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

Parameters

applicationMessage MqttApplicationMessage

The message to publish

cancellationToken CancellationToken

Cancellation token.

Returns

Task<MqttClientPublishResult>

The result of the publish.

Remarks

The behavior of publishing when the MQTT client is disconnected will vary depending on the implementation.

SubscribeAsync(MqttClientSubscribeOptions, CancellationToken)

Subscribe to a topic on the MQTT broker.

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

Parameters

options MqttClientSubscribeOptions

The details of the subscribe.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<MqttClientSubscribeResult>

The MQTT broker's response.

Remarks

The behavior of subscribing when the MQTT client is disconnected will vary depending on the implementation.

UnsubscribeAsync(MqttClientUnsubscribeOptions, CancellationToken)

Unsubscribe from a topic on the MQTT broker.

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

Parameters

options MqttClientUnsubscribeOptions

The details of the unsubscribe request.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<MqttClientUnsubscribeResult>

The MQTT broker's response.

Remarks

The behavior of unsubscribing when the MQTT client is disconnected will vary depending on the implementation.

Events

ApplicationMessageReceivedAsync

The event that notifies you when this client receives a PUBLISH from the MQTT broker.

event Func<MqttApplicationMessageReceivedEventArgs, Task>? ApplicationMessageReceivedAsync

Event Type

Func<MqttApplicationMessageReceivedEventArgs, Task>

Remarks

Users are responsible for acknowledging each received PUBLISH. Users must either automatically acknowledge the message by opting into automatically sending the acknowledgement by setting the AutoAcknowledge flag, or they can manually acknowledge the PUBLISH by invoking AcknowledgeAsync(CancellationToken).

Note that this client sends PUBLISH acknowledgements in the order that the corresponding PUBLISH packets were received, so failing to acknowledge a PUBLISH will block sending acknowledgements for all subsequent PUBLISH packets received.