Skip to main content

MQTT

MQTT event integrations subscribe to one or more MQTT topics and trigger an onMessage handler as each message arrives. Use them for IoT data ingestion, sensor readings, and lightweight publish/subscribe messaging where low bandwidth and low latency are priorities.

Creating an MQTT service

  1. Click + Add Artifact in the canvas or click + next to Entry Points in the sidebar.

  2. In the Artifacts panel, select MQTT under Event Integration.

  3. In the creation form, fill in the following fields:

    MQTT Event Integration creation form

    FieldDescriptionDefault
    Service URIURI of the MQTT broker (e.g., tcp://localhost:1883).Required
    Client IDUnique identifier for this client as recognised by the MQTT broker.Required
    SubscriptionsTopic or topics to subscribe to.Required

    Expand Advanced Configurations to set the listener name.

    FieldDescriptionDefault
    Listener NameIdentifier for the listener created with this service.mqttListener
  4. Click Create.

  5. WSO2 Integrator opens the service in the Service Designer. The canvas shows the attached listener pill and the Event Handlers section.

    Service Designer showing the MQTT service canvas

  6. Click + Add Handler to add the onMessage handler.

Listener configuration

In the Service Designer, click the Configure icon in the header to open the MQTT Event Integration Configuration panel. Select mqttListener under Attached Listeners to configure the listener.

MQTT Event Integration Configuration panel

FieldDescriptionDefault
NameIdentifier for the listener.mqttListener
Server URIURI of the remote MQTT server (e.g., tcp://localhost:1883).Required
Client IdUnique client ID to identify this listener to the broker.Required
SubscriptionsTopics to subscribe to.Required
Connection ConfigAdditional connection configuration as a record expression (e.g., credentials, keep-alive settings).{}
Manual AcksWhen set to true, messages must be manually acknowledged. When false, messages are automatically acknowledged.false

Click + Attach Listener to attach an additional listener to the same service.

Click Save Changes to apply updates.

Event handlers

MQTT services support a single handler type — onMessage — which is called for every message received on the subscribed topics.

Adding an event handler

In the Service Designer, click + Add Handler. The Select Handler to Add panel lists onMessage. Click it to add the handler directly — no additional configuration is required.

Message type

The onMessage handler receives an mqtt:Message parameter with the message content and metadata.

FieldTypeDescription
payloadbyte[]The payload of the message as a byte array
qosintQuality of service. 0 - at most once, 1 - at least once, 2 - exactly once
retainedbooleanIndicates whether this message should/is retained by the server
duplicatebooleanIndicates whether or not this message might be a duplicate
messageIdint?The message ID of the message. This is only set on messages received from the server
topicstring?The topic this message was received on. This is only set on messages received from the server
propertiesmqtt:MessageProperties?The properties of the message

QoS levels

LevelGuarantee
0Fire and forget — no acknowledgment
1Acknowledged delivery — possible duplicates
2Four-step handshake — no duplicates

What's next

  • RabbitMQ — consume messages from RabbitMQ queues
  • Kafka — consume messages from Apache Kafka topics
  • Connections — reuse MQTT connection credentials across services