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. For this demo, we are using the open Eclipse iot.eclipse.com mqtt sandbox server. Please see http://iot.eclipse.org/sandbox.html for further details about this sandbox server.

Running The Samples in StreamBase Studio

  1. In the Package Explorer, double-click to open Demo.sbapp. Make sure the application is the currently active tab in the EventFlow Editor.

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

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

    Enter the following values:

    1. Topic: Streambase2

    2. Payload: This is a test message

    3. Qos: 2

    And click the Send Data button.

  4. 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 setup to only handle Qos level 0 messages.

  5. In the Application Output view, observe tuples emitted on the PublishStatus and Data output streams.

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

  7. When done, press F9 or click the Stop Running Application button.

Running This Sample in Terminal Windows

This section describes how to run this sample in UNIX terminal windows or Windows command prompt windows.

On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.

  1. Open four terminal windows on UNIX, or four StreamBase Command Prompts on Windows. In each window, navigate to your workspace copy of the sample, as described in Sample Location below.

  2. In window 1, type:

    sbd -f sbd.sbconf Demo.sbapp
    
  3. In window 2, type:

    sbc dequeue Data
    

    This window will display tuples dequeued from the subscribe adapter's data stream.

  4. In window 3, type:

    sbc dequeue PublishStatus
    

    This window will display tuples dequeued from the publish adapter's status stream.

  5. In window 4, type:

    sbc enq PublishData
    Streambase2,test,2
    
  6. Observe that in window 2 and window 3 tuples are emitted from the PublishStatus and Data streams.

  7. Enter further test tuples in window 4 as desired.

  8. When done, in window 4, type Ctrl+C, then the following command to terminate the server and dequeuer:

    sbadmin shutdown
    

Importing This Sample into StreamBase Studio

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

  • From the top menu, select FileLoad StreamBase Sample.

  • Type mqtt to narrow the list of samples.

  • Select MQTT Client Adapters from the StreamBase Standard Adapters category.

  • Click OK.

StreamBase Studio creates a single project containing the sample files.

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 the permission problems that can occur when trying to work with the initially installed location of the sample. The default workspace location for this sample is:

studio-workspace/sample_adapter_embedded_mqtt-client

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

In the default TIBCO StreamBase installation, this sample's files are initially installed in:

streambase-install-dir/sample/adapter/embedded/mqtt-client

See Default Installation Directories for the location of streambase-install-dir on your system. This location may require administrator privileges for write access, depending on your platform.