Skip to main content

mqttclient.onmessage(callback)

Register a handler for incoming messages

Availability

Agent

Parameters

Name Type Description
callback Function Function to be called upon receipt of a message

Returns

Nothing

Description

This method allows you to register a function that will be called whenever a message is received from a topic to which the mqttclient has subscribed. The mqttclient will not receive messages posted to topics to which it has not subscribed.

The provided function must have one parameter of its own: item. This will be passed a table containing the following keys:

Key Type Description
topic String The name of the topic to which the message was posted
message Blob The message body
qos Integer The Quality of Service (QoS) level at which the message was delivered
retain Integer Indicates whether the delivered message was a ‘retained’ or not. 1 indicates the message was retained, 0 that it was not
dup Integer Indicates whether the broker had to retry sending the message and so it may be a duplicate. 1 indicates the message may be a duplicate, 0 that it may not

Only one callback function may be registered at any one time. To clear a previously registered callback, either provide a new function, or pass in null.

Note Inbound messages are now subject to rate limits.