Send a message to an MQTT client
WebSphere® MQ applications can send MQTT v3 clients messages by publishing to subscriptions created by clients, or by sending messages directly. MQTT clients can send messages to one another by publishing to topics subscribed to by other clients.
An MQTT client subscribes to a publication, which it receives from WebSphere MQ
Do the task, Publishing a message to the MQTT client utility from IBM WebSphere MQ Explorer to send a publication from WebSphere MQ to an MQTT client.
The standard way for an MQTT v3 client to receive
messages is for it to create a subscription to a topic, or set of
topics. In the example code snippet, Figure 1,
the MQTT client subscribes using the topic string "MQTT Examples"
.
A WebSphere MQ C application, Figure 2, publishes to the topic using the topic
string "MQTT Examples"
. In the code snippet Figure 3, the MQTT client receives the publication
in the callback method, messageArrived.
For further information about how to configure WebSphere MQ to send publications in response to subscriptions from MQTT clients, see Publishing a message in response to an MQTT client subscription .
A WebSphere MQ application sends a message directly to an MQTT client
Do the task, Sending a message to an MQTT client using IBM WebSphere MQ Explorer to send a message directly from WebSphere MQ to an MQTT client.
A message sent in this way to an MQTT client is called an unsolicited message. MQTT v3 clients receive unsolicited messages as publications with a topic name set. The telemetry (MQXR) service sets the topic name to the remote queue name.
You cannot send unsolicited messages to the WebSphere MQ daemon for devices: the daemon might shut down if it receives an unsolicited message. An MQTT v3 client cannot send an unsolicited message to another MQTT v3 client, nor to a WebSphere MQ queue.
For further information about how to configure WebSphere MQ to send messages directly to MQTT clients, see Sending a message to a client directly.
An MQTT client publishes a message
An MQTT v3 client can publish a message that is received by another MQTT v3 client, but it cannot send an unsolicited message. The code snippet, Figure 4 shows how an MQTT v3 client, written in Java, publishes a message.
The typical pattern for sending a message to one specific MQTT v3 client, is for each client to create a subscription to its own ClientIdentifier. Do the task, Publish a message to a specific MQTT v3 client, to publish a message from one MQTT client to another MQTT client using ClientIdentifier as a topic string.
Example code snippets
The code snippet in Figure 1 shows how an MQTT client written in Java creates a subscription. It also needs a callback method, messageArrived to receive publications for the subscription. The code snippet is extracted from the task, Creating a subscriber for MQ Telemetry Transport using Java.
The code snippet in Figure 2 shows how an WebSphere MQ application written in C sends a publication. The code snippet is extracted from the task, Create a publisher to a variable topic
When the publication arrives, the MQTT client calls the messageArrived method of the MQTT application client MqttCallback class. The code snippet is extracted from the task, Creating a subscriber for MQ Telemetry Transport using Java.
Figure 4 shows an MQTT v3 publishing a message to the subscription created in Figure 1. The code snippet is extracted from the task, Creating your first MQ Telemetry Transport publisher application using Java.