Publish

This MQTT Publish function publishes the message by topic and payload via the MQTT broker and returns a true or false if the action succeeded. Publishing requires the topic (subject), payload (content/message), quality of service (QOS), and retain parameters.

The QoS is used to agree on the level of guaranteed message delivery between the sender and receiver. This is important since MQTT manages the re-transmission of messages and will guarantees delivery (even when the underlying transport is not reliable), QoS makes communication in unreliable networks a lot easier. The MQTT has three QoS levels.

The Retain parameter stores the last published message in the broker and sends the last good message to new subscribers when they come online and subscribe to a new topic. By receiving the retained message as soon as the new subscriber comes online, the subscriber will not be in the dark wondering if their subscription is working or not. This also eliminates the amount of time a subscriber waits for the next updated topic.

Group:  MQTT Object

Type: Function

Syntax:   oMQTT.Publish( Topic, Payload, QOS, Retain) As Boolean

Publish      (Boolean) returns true if publish succeeded, false if it failed

Topic        (String) The topic (subject) of the message

Payload   (Long) the time in seconds a client waits before the connection is closed

QoS          (Long) QOS 0: At most once (deliver and forget); QOS 1: At lease once; QOS 2: Exactly once

Retain     (Boolean) True sets the message to be retained. False will not retain the message.

Example:

Dim WithEvents moClient As New MQTT

Private Sub btnPublish_Click()

On Error Resume Next

Dim sIndex As String

sIndex = Split(cboQOS.Text, " ")(0)

If Not moClient.Publish(txtTopic.Text, txtPayload.Text, CLng(sIndex), chkRetain.Checked) Then

App.MsgBox("Error: " & moClient.Error)

Exit Sub

Else

App.MsgBox("Published to " & txtTopic.Text)

End If

End Sub

 

Versions Supported:      RFgen 5.1.1.35 and 5.2.3 and newer.