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
brokerHostNamestringThe 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
brokerHostNamestringThe Broker Host Name or IP Address.
brokerPortintThe Broker port.
secureboolA value indicating whether to use a secure connection (SSL/TLS).
caCertX509CertificateThe CA (Certificate Authority) certificate for secure connection.
clientCertX509CertificateThe client certificate for secure connection.
sslProtocolMqttSslProtocolsThe 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
CleanSession
Clean session flag
public bool CleanSession { get; }
Property Value
ClientId
Client identifier
public string ClientId { get; }
Property Value
IsAuthenticationFlow
Set to true if you have an authentication flow, v5.0 only
public bool IsAuthenticationFlow { get; set; }
Property Value
IsConnected
Connection status between client and broker
public bool IsConnected { get; }
Property Value
MaximumPacketSize
Maximum Packet Size, v5.0 only
public uint MaximumPacketSize { get; set; }
Property Value
ProtocolVersion
MQTT protocol version
public MqttProtocolVersion ProtocolVersion { get; set; }
Property Value
ReceiveMaximum
ReceiveMaximum, v5.0 only
public ushort ReceiveMaximum { get; set; }
Property Value
RequestProblemInformation
Request Problem Information, v5.0 only
public bool RequestProblemInformation { get; set; }
Property Value
RequestResponseInformation
Request Response Information, v5.0 only
public bool RequestResponseInformation { get; set; }
Property Value
SessionExpiryInterval
Session Expiry Interval, v5.0 only
public uint SessionExpiryInterval { get; set; }
Property Value
Settings
MQTT client settings
public MqttSettings Settings { get; }
Property Value
TopicAliasMaximum
TopicAliasMaximum, v5.0 only
public ushort TopicAliasMaximum { get; set; }
Property Value
UserProperties
User Property, v5.0 only
public ArrayList UserProperties { get; }
Property Value
WillDelayInterval
Will Delay Interval, v5.0 only
public uint WillDelayInterval { get; set; }
Property Value
WillFlag
Will flag
public bool WillFlag { get; }
Property Value
WillMessage
Will message
public string WillMessage { get; }
Property Value
WillQosLevel
Will QOS level
public MqttQoSLevel WillQosLevel { get; }
Property Value
WillTopic
Will topic
public string WillTopic { get; }
Property Value
Methods
Close()
Close client
public void Close()
Connect(string)
Connect to broker
public MqttReasonCode Connect(string clientId)
Parameters
clientIdstringClient identifier
Returns
- MqttReasonCode
Return code of CONNACK message from broker
Connect(string, bool)
Connect to broker
public MqttReasonCode Connect(string clientId, bool cleanSession)
Parameters
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
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
clientIdstringClient identifier
usernamestringUsername
passwordstringPassword
cleanSessionboolClean sessione flag
keepAlivePeriodushortKeep 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
clientIdstringClient identifier
usernamestringUsername
passwordstringPassword
willRetainboolWill retain flag
willQosLevelMqttQoSLevelWill QOS level
willFlagboolWill flag
willTopicstringWill topic
willMessagestringWill message
cleanSessionboolClean sessione flag
keepAlivePeriodushortKeep 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
brokerHostNamestringBroker Host Name or IP Address
brokerPortintBroker port
secureboolUsing secure connection
caCertbyte[]CA certificate for secure connection
clientCertbyte[]Client certificate
sslProtocolMqttSslProtocolsSSL/TLS protocol version
Publish(string, byte[])
Publish a message asynchronously (QoS Level AtMostOnce and not retained).
public ushort Publish(string topic, byte[] message)
Parameters
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
topicstringMessage topic.
messagebyte[]Message data (payload).
contentTypestringContent 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
topicstringMessage topic.
messagebyte[]Message data (payload).
contentTypestringContent of the application message. This is only available for MQTT v5.0.
userPropertiesArrayListUser properties for the application message. This is only available for MQTT v5.0
Returns
- ushort
Message Id related to PUBLISH message.
Exceptions
- ArgumentException
If
userPropertieselements 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
topicstringMessage topic.
messagebyte[]Message data (payload).
contentTypestringContent of the application message. This is only available for MQTT v5.0.
userPropertiesArrayListUser properties for the application message. This is only available for MQTT v5.0
qosLevelMqttQoSLevelQoS Level.
retainboolRetain flag.
Returns
- ushort
Message Id related to PUBLISH message.
Exceptions
- ArgumentException
If
userPropertieselements 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
topicsstring[]List of topics to subscribe
qosLevelsMqttQoSLevel[]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
topicsstring[]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
ConnectionClosed
The event for peer/client disconnection
public event IMqttClient.ConnectionClosedEventHandler ConnectionClosed
Event Type
ConnectionClosedRequest
The event for peer/client disconnection
public event MqttClient.ConnectionClosedRequestEventHandler ConnectionClosedRequest
Event Type
ConnectionOpened
The event for peer/client disconnection
public event MqttClient.ConnectionOpenedEventHandler ConnectionOpened
Event Type
MqttMsgPublishReceived
The event for PUBLISH message received
public event IMqttClient.MqttMsgPublishEventHandler MqttMsgPublishReceived
Event Type
MqttMsgPublished
The event for published message
public event IMqttClient.MqttMsgPublishedEventHandler MqttMsgPublished
Event Type
MqttMsgSubscribed
The event for subscribed topic
public event IMqttClient.MqttMsgSubscribedEventHandler MqttMsgSubscribed
Event Type
MqttMsgUnsubscribed
The event for unsubscribed topic
public event IMqttClient.MqttMsgUnsubscribedEventHandler MqttMsgUnsubscribed