TsgcWSPClient_MQTT

The MQTT component provides a lightweight, fully-featured MQTT client implementation with support for versions 3.1.1 and 5.0. The component supports plaintext and secure connections over both standard TCP and WebSockets.

 

Connection to a MQTT server is simple, you need to drop this component in the form and select a TsgcWebSocketClient Component using Client Property. Set host and port in TsgcWebSocketClient and set Active := True to connect.

 

MQTT v5.0 is not backward compatible (like v3.1.1). Obviously too many new things are introduced so existing implementations have to be revisited.

 

According to the specification, MQTT v5.0 adds a significant number of new features to MQTT while keeping much of the core in place.

 

 

Methods

  Connect: this method is called automatically after a successful WebSocket connection.

 

  Ping: Sends a ping to the server, usually to keep the connection alive. If you enable HeartBeat property, ping will be sent automatically by a defined interval.

 

  Subscribe: subscribe client to a custom channel. If the client is subscribed, OnMQTTSubscribe event will be fired.

SubscribeProperties: (New in MQTT 5.0)

 

 

Example:

 


oProperties := TsgcWSMQTTSubscribe_Properties.Create;
Try
  oProperties.SubscriptionIdentifier := 16385;
  MQTT.Subscribe('myChannel', mtqsAtMostOnce, oProperties);
Finally
  FreeAndNil(oProperties);
End;

  Unsubscribe: unsubscribe client to a custom channel. If the client is unsubscribed, OnMQTTUnsubscribe event will be fired.

UnsubscribeProperties: (New in MQTT 5.0)

 

 

Example:

 


oProperties := TsgcWSMQTTUnsubscribe_Properties.Create;
Try
  oProperties.UserProperties.Add('Temp=21');
  oProperties.UserProperties.Add('Humidity=55');
  MQTT.UnSubscribe('myChannel', mtqsAtMostOnce, oProperties);
Finally
  FreeAndNil(oProperties);
End;

 

  Publish: sends a message to all subscribed clients. There are the following parameters:

Topic: is the channel where the message will be published.

Text: is the text of the message.

QoS: is the Quality Of Service of published message. There are 3 possibilities:

 

mtqsAtMostOnce: (by default) the message is delivered according to the best efforts of the underlying TCP/IP network. A response is not expected and no retry semantics are defined in the protocol. The message arrives at the server either once or not at all.

mtqsAtLeastOnce: the receipt of a message by the server is acknowledged by an ACKNOWLEDGMENT message. If there is an identified failure of either the communications link or the sending device or the acknowledgement message is not received after a specified period of time, the sender resends the message. The message arrives at the server at least once. A message with QoS level 1 has an ID param in the message.

mtqsExactlyOnce: where message are assured to arrive exactly once. This level could be used, for example, with billing systems where duplicate or lost messages could lead to incorrect charges being applied. If there is an identified failure of either the communications link or the sending device, or the acknowledgement message is not received after a specified period of time, the sender resends the message.

 

Retain: if True, Server MUST store the Application Message and its QoS, so that it can be delivered to future subscribers whose subscriptions match its topic name. By default is False.

PublishProperties: (New in MQTT 5.0)

 

 

 PublishAndWait: is the same method than Publish, but in this case, if QoS is [mtqsAtLeastOnce, mtqsExactlyOnce] waits till server processes the message, this way, if you get a positive result, means that message has been received by server. There is a timeout of 10 seconds by default, if after the timeout there is no response from server, the response will be false.

 

  Disconnect: disconnects from MQTT server.

ReasonCode: code identifies reason why disconnects.(New in MQTT 5.0)

DisconnectProperties (New in MQTT 5.0)

 

 

  Auth:  is sent from Client to Server or Server to Client as part of an extended authentication exchange, such as challenge / response authentication. (New in MQTT 5.0)

ReAuthenticate: if True Initiate a re-authentication, otherwise continue the authentication with another step.

AuthProperties

 

 

 

 

 

Events

  OnMQTTBeforeConnect: this event is triggered before a new connection is established. There are 2 parameters:

CleanSession: if True (by default), the server must discard any previous session and start a new session. If false, the server must resume communication.

ClientIdentifier: every new connection needs a client identifier, this is set automatically by component, but can be modified if needed.

 

  OnMQTTConnect: this event is triggered when the client is connected to MQTT server. There are 2 parameters:

Session:

  1. If client sends a connection with CleanSession = True, then Server Must respond with Session = False.

  2. If client sends a connection with CleanSession = False:

 

ReasonCode: returns code with the result of connection.(New in MQTT 5.0)

ReasonName: text description of ReturnCode.(New in MQTT 5.0)

ConnectProperties: (New in MQTT 5.0)

 

 

  OnQTTDisconnect: this event is triggered when the client is disconnected from MQTT server. Parameters:

ReasonCode: returns code with the result of connection.(New in MQTT 5.0)

ReasonName: text description of ReturnCode.(New in MQTT 5.0)

DisconnectProperties: (New in MQTT 5.0)

 

 

  OnMQTTPing: this event is triggered when the client receives an acknowledgment from a ping previously sent.

 

  OnMQTTPubAck: this event is triggered when receives the response to a Publish Packet with QoS level 1. There is one parameter:

PacketIdentifier: is packet identifier sent initially.

ReasonCode: returns code with the result of connection.(New in MQTT 5.0)

ReasonName: text description of ReturnCode.(New in MQTT 5.0)

PubAckProperties: (New in MQTT 5.0)

 

 

  OnMQTTPubComp: this event is triggered when receives the response to a PubRel Packet. It is the fourth and final packet of the QoS 2 protocol exchange. There are the following parameters:

PacketIdentifier: is packet identifier sent initially.

ReasonCode: returns code with the result of connection.(New in MQTT 5.0)

ReasonName: text description of ReturnCode.(New in MQTT 5.0)

PubCompProperties: (New in MQTT 5.0)

 

 

  OnMQTTPublish: this event is triggered when the client receives a message from the server. There are 2 parameters:

Topic: is the topic name of the published message.

Text: is the text of the published message.

PublishProperties: (New in MQTT 5.0)

 

 

  OnMQTTPubRec: this event is triggered when receives the response to a Publish Packet with QoS 2. It is the second packet of the QoS 2 protocol exchange. There are the following parameters:

PacketIdentifier: is packet identifier sent initially.

ReasonCode: returns code with the result of connection.(New in MQTT 5.0)

ReasonName: text description of ReturnCode.(New in MQTT 5.0)

PubRecProperties: (New in MQTT 5.0)

 

 

  OnMQTTSubscribe: this event is triggered as a response to subscribe method. There are the following parameters:

PacketIdentifier: is packet identifier sent initially.

Codes: codes with the result of a subscription.

SubscribeProperties: (New in MQTT 5.0)

 

 

 

  OnMQTTUnSubscribe: this event is triggered as a response to subscribe method. There are the following parameters:

PacketIdentifier: is packet identifier sent initially.

Codes: codes with the result of a subscription.

UnsubscribeProperties: (New in MQTT 5.0)

 

 

  OnMQTTAuth: this event is triggered as a response to Äuth method. There is one parameter: (New in MQTT 5.0)

ReasonCode: returns code with the result of connection.

ReasonName: text description of ReturnCode.

AuthProperties:

 

 

 

Enhanced Authentication (New in MQTT 5.0)

 

To begin an enhanced authentication, the Client includes an Authentication Method in the ConnectProperties. This specifies the authentication method to use. If the Server does not support the Authentication Method supplied by the Client, it may send a Reason Code "Bad authentication method" or Not Authorized.

 

Example:

 

 

Properties

   MQTTVersion: select which MQTT version (3.1.1 or 5.0) will use to connect to server.

 

   Authentication: disabled by default, if True a UserName and Password are sent to the server to try user authentication.

 

   HeartBeat: enabled by default, if True, send a ping every X seconds (set by Interval property) to keep alive connection. You can set a Timeout too, so if after X seconds, the client doesn't receive a response to a ping, the connection will be closed automatically.

 

   LastWillTestament: if there is a disconnection and is enabled, a message is sent to all connected clients to inform that connection has been closed.

 

 

   ConnectProperties: (New in MQTT 5.0) are connection properties sent with packet connect.