MQTT Client Publish And Subscribe Samples

About The Samples

In this sample, the TIBCO StreamBase® Output Adapter for MQTT Client Subscribe and TIBCO StreamBase® Input Adapter for MQTT Client Publish are used together to subscribe to and send data to an MQTT broker. The sample will initially subscribe to three message queues (Streambase0, Streambase1, Streambase2), each corresponding to their quality of service number.

The sample server configuration file in this sample contains connection information that is shared between the subscribe and publish adapters. This demo uses the open Eclipse iot.eclipse.org MQTT sandbox server. Please see http://iot.eclipse.org/sandbox.html for further details about this server.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top-level menu, select File>Import Samples and Community Content.

  • Enter mqtt to narrow the list of options.

  • Select MQTT client publish and subscribe adapters from the StreamBase Messaging Adapters category.

  • Click Import Now.

StreamBase Studio creates a single project containing the sample files.

Running the Demo Sample

  1. In the Project Explorer view, open the sample you just loaded.

    If you see red marks on a project folder, wait a moment for the project to load its features.

    If the red marks do not resolve themselves after a minute, select the project, right-click, and select Maven>Update Project from the context menu.

  2. Open the src/main/eventflow/packageName folder.

  3. Open the demo.sbapp file and click the Run button. This opens the SB Test/Debug perspective and starts the module.

  4. Select the PublishData Stream in the Manual Input window.

    Enter the following values:

    • Topic: Streambase2

    • Payload: This is a test message

    • Qos: 2

    • Advanced: null

    and click the Send Data button.

  5. Repeat this step, sending messages to the various message queues. Note that Qos can only be set as high as the message queue default allows. For example, you cannot send a Qos level 2 message to Streambase0 because that message queue was set up to only handle Qos level 0 messages.

  6. In the Output Streams view, observe tuples emitted on the PublishStatus and Data output streams.

  7. Select individual tuples emitted from the PublishStatus and Data streams, to analyze and understand their structure.

  8. When done, press F9 or click the Terminate EventFlow Fragment button.

Running the Demo Manual Ack Sample

It is important to note that MQTT requires various settings in order to set up persistent connections:

  • A ClientId set in the src/main/configurations/MQTT.conf connection settings file. If left blank, a default is used, and persistence will not service program restarts.

  • The isCleanSession setting must be set to false in the connection settings file.

  • The manualAcks setting must be set to true.

  • Messages must be set to QoS level 2.

  • Messages must be ACKed by the MQTTClientAck adapter or they will be assumed to be undelivered and delivered during the next connection (this sample demonstrates this).

  • If you plan to stop your running process and restart, change PersistenceType to file in the settings file to maintain state across program restarts.

To run the sample, do the following:

  1. Continuing in the same sample project, open the DemoManualAck.sbapp file.

  2. Click the Run button. This opens the SB Test/Debug perspective and starts the module.

  3. This sample automatically connects to the iot.eclipse.org MQTT broker and sends five messages to the channel, StreambaseAck. The MQTT Client Subscribe adapter acknowledges three of the five and then disconnect, reconnect, and acknowledges the remaining two.

  4. In the Output Streams view, observe tuples emitted on the PublishStatus and Data output streams.

  5. When done, press F9 or click the Terminate EventFlow Fragment button.

Sample Location

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

Important

Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids permission problems. The default workspace location for this sample is:

studio-workspace/sample_adapter_embedded_mqtt-client

See Default Installation Directories for the default location of studio-workspace on your system.