Skip to main content

device.send(messageName, data)

Posts a message from the agent to the device






Name Type Description
messageName String The name of the message
data Any The data to be passed to the device


This method works in partnership with agent.on() to allow messages and data to be sent from the agent to its imp. For example, if the agent calls:

device.send("setspeed", data);

and the device has already called:

agent.on("setspeed", messageHandler);

then on receipt of the message setspeed from the agent, the device’s messageHandler() function will be called and be passed the contents of the variable data, which can be any serializable Squirrel table, array, string, integer, float or boolean. See the Developer Guide ‘Serializable Squirrel’ for Squirrel’s data serialization rules. The message handler function will always be called with exactly one parameter: the data passed to device.send(). The data parameter in both messageHandler() and device.send() is not optional. You must include a value, even if you do not plan to use it in the handler.

Example Code

The following agent code’s startTime() function uses device.send() to issue a ‘pong’ message to the device in response to the arrival of a ‘ping’ message from the device.