Skip to content

ClientConnection Public API

Eric de La Rochette edited this page Oct 8, 2017 · 6 revisions

Reference

Name ClientConnection
Full Path MQTTLib.ClientConnection
Super Object
Implements n/a

Events

BrokerConnected(inSessionPresentFlag As Boolean)

The broker/server and the client are connected i.e. the client can publish or subscribe to topics. If the connection was established with the CleanSession flag set to 0, the inSessionPresent parameter will be True if the server has a stored session state from a previous connection with the same client id. Otherwise inSessionPresent is False

BrokerConnectionRejected(inErrorCode As Integer)

The server rejected the connection. Use the constants from MQTTLib.OptionsCONNACK to identify the reason of the rejection.

Error(inMessage As String, inError As MQTTLib.Error)

An error has occurred. Use the MQTTLib.Error enumeration to identify the reason from the value of inError.

ReceivedPINGRESP()

The client received a PINGRESP packet. This is part of the keep alive connection.

ReceivedPUBACK(inPacketID As UInt16)

Part of the At Least Once Delivery QoS flow.

The PUBACK packet is sent by the server to acknowledge the reception of a PUBLISH packet sent with the At Least Once Delivery QoS. There is no further process needed.

ReceivedPUBCOMP(inPacketID As UInt16)

Part of the Exactly Once Delivery QoS flow

The PUBCOMP is the last packet sent when using the Exactly Once Delivery QoS. It means that the entire transaction went well and is now over.

ReceivedPUBLISH(inPublish As MQTTLib.OptionsPUBLISH) As Boolean

The server sent a PUBLISH packet. The message topic, QoS and content are provided by inPublish's properties. Return Falseif you want to let the client handle the QoS flow by itself.

If you return True You'll have to handle the reply to the server yourself if needed:

  • At Most Once Delivery QoS level: No further action is needed.
  • At Least Once Delivery QoS level: A single PUBACK with the same packet id will close the exchange.
  • Exactly Once Delivery QoS Level: You'll have to send a PUBREC packet with the same packet ID than the PUBLISH packet and handle the next PUBREL packet send by the server in ReceivedPUBREL() event.

If you want more informations about MQTT's Quality of Service (QoS) flows, click here and go to section 4.3.

ReceivedPUBREC(inPacketID As UInt16) As Boolean

Part of the Exactly Once Delivery QoS flow

The server received a PUBLISH packet identified by inPacketID and sends back a PUBREC packet with the same id. Return True if you want to handle the QoS flow by yourself, which includes sending the PUBREL packet before the server's timeout.

ReceivedPUBREL(inPacketID As UInt16) As Boolean

Part of the Exactly Once Delivery QoS flow

The server has received a PUBREC packet identified by inPacketID and replies with a PUBREL packet with the same id. Return True if you want to handle the QoS flow by yourself, which includes sending the final PUBCOMP with the same id within the server's time out. If you returtn False or no value, the client will send the reply itself.

ReceivedSUBACK(inSUBACKData As MQTTLib.OptionsSUBACK)

The server has received a SUBSCRIBE packet and replies with a SUBACK packet with the same id and containing data about the subscriptions topic by topic. See here in section 3.9.

ReceivedUNSUBACK(inPacketID As UInt16)

The server acknowledges the reception of an UNSUBSCRIBE packet identified by inPacketID.

Properties

Connected As Boolean

Read only

This property is True when the client is connected to the server, False otherwise.

Methods

Connect()

Start the connection process to the server. When the connection is effective, the BrokerConnected event is triggered. If the transport connection failed or the broker rejected the connection the BrokerConnectionRejected is triggered. Use the MQTTLib.OptionsCONNACK constants to interpret the value of the parameter inErrorCode.

Disconnect()

Use this method to perform a clean disconnection. The client will close both MQTT and transport connections.

EasyTCPConnect(inHost As String, inPort As Integer, inClientID As String, inCleanSession As Boolean, inKeepAlive As UInt16)

This is the simplest way to setup and initiate an unsecured TCP connection with no last will and credentials. You just have to provide the host's address, the TCP port to use, the clientID, the CleanSession flag and the KeepAlive time. To See more detaiils about these properties, click here and go to section 3.1.

NewPacketID() As UInt16

Generate an unused packet id. Using this method each time you need a packet id will avoid any conflicts with an already in use packet id.

PingBroker()

Calling this method will send a PING packet to the server. If no PINGRESP if received within the allowed time out, the Error event will be triggered with inError set to MQTTLib.Error.PINGTimedOut.

Publish(inOptions As MQTTLib.OptionsPUBLISH)

Use this method to publish messages.

// MQTTClient is a ClientConnectio control dropped in a window

Dim theMessage As New MQTTLib.OptionsPUBLISH
 		  
theMessage.Message = "Hello World"
theMessage.TopicName = "Test/World"
theMessage.QoSLevel = MQTTLib.QoS.AtLeastOnceDelivery

// Set the packet ID. You may need to store the value for further use.
Dim thePacketID As UInt16 = Self.MQTTClient.NewPacketID
theMessage.PacketID = thePacketID

// Publish the message
Self.MQTTClient.Publish theMessage

SendPUBACK(inPacketID As UInt16)

Used to send a PUBACK packet in response to a PUBLISH packet using At Least Once Delivery QoS. If the packet id is 0, an MQTTLib.ProtocolException will be raised.

Setup(inSocketAdapter As MQTTLib.SocketAdapter, inConnectionSetup As MQTTLib.OptionsCONNECT)

You have to call this method before connecting to provide a SocketAdapter and the setup of the CONNECT packet.

This step is not needed if you're using the EasyTCPConnect to connect to the server.

Subscribe(inOptions As MQTTLib.OptionsSUBSCRIBE)

Use this method to subscribe to one of more topics. inOptions will provide the topic name(s) or filter(s), the requested QoS(s) and a packet id which will help to identify the SUBACK packet the server will send back. If you don't provide a packet id, the client will assigned one automatically.

Unsubscribe(inOptions As MQTTLib.OptionsUNSUBSCRIBE)

Use this method to unsubscribe to one of more topics. inOptions will provide the topic name(s) or filter(s) and a packet id which will help to identify the SUBACK packet the server will send back. If you don't provide a packet id, the client will assigned one automatically.