Table of Contents

Class MqttClient

Namespace
nanoFramework.M2Mqtt
Assembly
nanoFramework.M2Mqtt.dll

MQTT Client

public class MqttClient : IMqttClient, IDisposable
Inheritance
MqttClient
Implements
Inherited Members
Extension Methods

Constructors

MqttClient(string)

Initializes a new instance of the MqttClient class with default port and non-secure connection.

public MqttClient(string brokerHostName)

Parameters

brokerHostName string

The Broker Host Name or IP Address.

MqttClient(string, int, bool, X509Certificate, X509Certificate, MqttSslProtocols)

Initializes a new instance of the MqttClient class.

public MqttClient(string brokerHostName, int brokerPort, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol)

Parameters

brokerHostName string

The Broker Host Name or IP Address.

brokerPort int

The Broker port.

secure bool

A value indicating whether to use a secure connection (SSL/TLS).

caCert X509Certificate

The CA (Certificate Authority) certificate for secure connection.

clientCert X509Certificate

The client certificate for secure connection.

sslProtocol MqttSslProtocols

The SSL/TLS protocol to use.

Properties

AuthenticationData

Authentication Data, v5.0 only

public byte[] AuthenticationData { get; set; }

Property Value

byte[]

AuthenticationMethod

Authentication Method, v5.0 only

public string AuthenticationMethod { get; set; }

Property Value

string

CleanSession

Clean session flag

public bool CleanSession { get; }

Property Value

bool

ClientId

Client identifier

public string ClientId { get; }

Property Value

string

IsAuthenticationFlow

Set to true if you have an authentication flow, v5.0 only

public bool IsAuthenticationFlow { get; set; }

Property Value

bool

IsConnected

Connection status between client and broker

public bool IsConnected { get; }

Property Value

bool

MaximumPacketSize

Maximum Packet Size, v5.0 only

public uint MaximumPacketSize { get; set; }

Property Value

uint

ProtocolVersion

MQTT protocol version

public MqttProtocolVersion ProtocolVersion { get; set; }

Property Value

MqttProtocolVersion

ReceiveMaximum

ReceiveMaximum, v5.0 only

public ushort ReceiveMaximum { get; set; }

Property Value

ushort

RequestProblemInformation

Request Problem Information, v5.0 only

public bool RequestProblemInformation { get; set; }

Property Value

bool

RequestResponseInformation

Request Response Information, v5.0 only

public bool RequestResponseInformation { get; set; }

Property Value

bool

SessionExpiryInterval

Session Expiry Interval, v5.0 only

public uint SessionExpiryInterval { get; set; }

Property Value

uint

Settings

MQTT client settings

public MqttSettings Settings { get; }

Property Value

MqttSettings

TopicAliasMaximum

TopicAliasMaximum, v5.0 only

public ushort TopicAliasMaximum { get; set; }

Property Value

ushort

UserProperties

User Property, v5.0 only

public ArrayList UserProperties { get; }

Property Value

ArrayList

WillDelayInterval

Will Delay Interval, v5.0 only

public uint WillDelayInterval { get; set; }

Property Value

uint

WillFlag

Will flag

public bool WillFlag { get; }

Property Value

bool

WillMessage

Will message

public string WillMessage { get; }

Property Value

string

WillQosLevel

Will QOS level

public MqttQoSLevel WillQosLevel { get; }

Property Value

MqttQoSLevel

WillTopic

Will topic

public string WillTopic { get; }

Property Value

string

Methods

Close()

Close client

public void Close()

Connect(string)

Connect to broker

public MqttReasonCode Connect(string clientId)

Parameters

clientId string

Client identifier

Returns

MqttReasonCode

Return code of CONNACK message from broker

Connect(string, bool)

Connect to broker

public MqttReasonCode Connect(string clientId, bool cleanSession)

Parameters

clientId string

Client identifier

cleanSession bool

Clean sessione flag

Returns

MqttReasonCode

Return code of CONNACK message from broker

Connect(string, string, string)

Connect to broker

public MqttReasonCode Connect(string clientId, string username, string password)

Parameters

clientId string

Client identifier

username string

Username

password string

Password

Returns

MqttReasonCode

Return code of CONNACK message from broker

Connect(string, string, string, bool, ushort)

Connect to broker

public MqttReasonCode Connect(string clientId, string username, string password, bool cleanSession, ushort keepAlivePeriod)

Parameters

clientId string

Client identifier

username string

Username

password string

Password

cleanSession bool

Clean sessione flag

keepAlivePeriod ushort

Keep alive period

Returns

MqttReasonCode

Return code of CONNACK message from broker

Connect(string, string, string, bool, MqttQoSLevel, bool, string, string, bool, ushort)

Connect to broker

public MqttReasonCode Connect(string clientId, string username, string password, bool willRetain, MqttQoSLevel willQosLevel, bool willFlag, string willTopic, string willMessage, bool cleanSession, ushort keepAlivePeriod)

Parameters

clientId string

Client identifier

username string

Username

password string

Password

willRetain bool

Will retain flag

willQosLevel MqttQoSLevel

Will QOS level

willFlag bool

Will flag

willTopic string

Will topic

willMessage string

Will message

cleanSession bool

Clean sessione flag

keepAlivePeriod ushort

Keep alive period

Returns

MqttReasonCode

Return code of CONNACK message from broker

Disconnect()

Disconnect from broker

public void Disconnect()

Dispose()

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

public void Dispose()

Init(string, int, bool, byte[], byte[], MqttSslProtocols)

MqttClient initialization

public void Init(string brokerHostName, int brokerPort, bool secure, byte[] caCert, byte[] clientCert, MqttSslProtocols sslProtocol)

Parameters

brokerHostName string

Broker Host Name or IP Address

brokerPort int

Broker port

secure bool

Using secure connection

caCert byte[]

CA certificate for secure connection

clientCert byte[]

Client certificate

sslProtocol MqttSslProtocols

SSL/TLS protocol version

Publish(string, byte[])

Publish a message asynchronously (QoS Level AtMostOnce and not retained).

public ushort Publish(string topic, byte[] message)

Parameters

topic string

Message topic.

message byte[]

Message data (payload).

Returns

ushort

Message Id related to PUBLISH message.

Publish(string, byte[], string)

Publish a message asynchronously (QoS Level AtMostOnce and not retained).

public ushort Publish(string topic, byte[] message, string contentType)

Parameters

topic string

Message topic.

message byte[]

Message data (payload).

contentType string

Content of the application message. This is only available for MQTT v5.0.

Returns

ushort

Message Id related to PUBLISH message.

Exceptions

NotSupportedException

If setting a parameter that is not supported in the MQTT version set for this MqttClient.

Publish(string, byte[], string, ArrayList)

Publish a message asynchronously (QoS Level AtMostOnce and not retained).

public ushort Publish(string topic, byte[] message, string contentType, ArrayList userProperties)

Parameters

topic string

Message topic.

message byte[]

Message data (payload).

contentType string

Content of the application message. This is only available for MQTT v5.0.

userProperties ArrayList

User properties for the application message. This is only available for MQTT v5.0

Returns

ushort

Message Id related to PUBLISH message.

Exceptions

ArgumentException

If userProperties elements aren't of type UserProperty.

NotSupportedException

If setting a parameter that is not supported in the MQTT version set for this MqttClient.

Publish(string, byte[], string, ArrayList, MqttQoSLevel, bool)

Publish a message asynchronously.

public ushort Publish(string topic, byte[] message, string contentType, ArrayList userProperties, MqttQoSLevel qosLevel, bool retain)

Parameters

topic string

Message topic.

message byte[]

Message data (payload).

contentType string

Content of the application message. This is only available for MQTT v5.0.

userProperties ArrayList

User properties for the application message. This is only available for MQTT v5.0

qosLevel MqttQoSLevel

QoS Level.

retain bool

Retain flag.

Returns

ushort

Message Id related to PUBLISH message.

Exceptions

ArgumentException

If userProperties elements aren't of type UserProperty.

NotSupportedException

If setting a parameter that is not supported in the MQTT version set for this MqttClient.

Subscribe(string[], MqttQoSLevel[])

Subscribe for message topics

public ushort Subscribe(string[] topics, MqttQoSLevel[] qosLevels)

Parameters

topics string[]

List of topics to subscribe

qosLevels MqttQoSLevel[]

QOS levels related to topics

Returns

ushort

Message Id related to SUBSCRIBE message

Unsubscribe(string[])

Unsubscribe for message topics

public ushort Unsubscribe(string[] topics)

Parameters

topics string[]

List of topics to unsubscribe

Returns

ushort

Message Id in UNSUBACK message from broker

Events

Authentication

The event for peer/client disconnection

public event MqttClient.MqttMsgAuthenticationEventHandler Authentication

Event Type

MqttClient.MqttMsgAuthenticationEventHandler

ConnectionClosed

The event for peer/client disconnection

public event IMqttClient.ConnectionClosedEventHandler ConnectionClosed

Event Type

IMqttClient.ConnectionClosedEventHandler

ConnectionClosedRequest

The event for peer/client disconnection

public event MqttClient.ConnectionClosedRequestEventHandler ConnectionClosedRequest

Event Type

MqttClient.ConnectionClosedRequestEventHandler

ConnectionOpened

The event for peer/client disconnection

public event MqttClient.ConnectionOpenedEventHandler ConnectionOpened

Event Type

MqttClient.ConnectionOpenedEventHandler

MqttMsgPublishReceived

The event for PUBLISH message received

public event IMqttClient.MqttMsgPublishEventHandler MqttMsgPublishReceived

Event Type

IMqttClient.MqttMsgPublishEventHandler

MqttMsgPublished

The event for published message

public event IMqttClient.MqttMsgPublishedEventHandler MqttMsgPublished

Event Type

IMqttClient.MqttMsgPublishedEventHandler

MqttMsgSubscribed

The event for subscribed topic

public event IMqttClient.MqttMsgSubscribedEventHandler MqttMsgSubscribed

Event Type

IMqttClient.MqttMsgSubscribedEventHandler

MqttMsgUnsubscribed

The event for unsubscribed topic

public event IMqttClient.MqttMsgUnsubscribedEventHandler MqttMsgUnsubscribed

Event Type

IMqttClient.MqttMsgUnsubscribedEventHandler