MQTTClient.onMessage()

Overview

Used to set or replace the general message handler for an MQTTClient object.

Added in version 2.1.13

Format

var mqttClient = new MQTTClient(options);

//...

mqttClient.onMessage = messageHandler;

Value Description Type Required Notes
messageHandler A callback function that is called when a message is received. Function Optional When called, the first parameter is a Message object as described below.

Message object properties

Value Description Type Notes
topic The topic for the message. String  
payload An ArrayBuffer containing the payload for the message. ArrayBuffer  
qos The quality of service for the message. Integer

Will use the lowest of either the subscription QoS level or the sent message QoS level.

Possible values:

0 – at most once, where messages are delivered according to the best efforts of the operating environment. Message loss can occur.

1 – at least once, where messages are assured to arrive but duplicates can occur.

2 – exactly once, where messages are assured to arrive exactly once.

retain A flag indicating if this message is a retained message Boolean  
payloadAsString The payload bytes decoded as a UTF-8 string. string  

Example

Copy
/* Connect and subscribe to the "toast/for/all" topic, and show a toast for each message.
 */

mqttClient = new MQTTClient({
    broker: "mqtt://mymqtt.example.com",
    subscribeTo: [ "toast/for/all" ]
});

mqttClient.connect();

mqttClient.onMessage = function(message) {
    if(message.topic === "toast/for/all") {
        View.toast(message.payloadAsString, true);
    }
};