Publishes a message to a specified topic.

publish(params,callback)

Parameter Type Description
params object The parameter object. For more information, see the table Parameter descriptions of params.
callback(err) function The callback function. The callback function must follow standard JavaScript practices.
  • If the call is successful, err is null.
  • If the call fails, err contains the error information.
Table 1. Parameter descriptions of params
Parameter Type Description
topic String The topic that receives the message.
payload String The message payload.

Sample requests

In the following example, each time an external event is triggered, a message is sent to the /topic/hello topic.

'use strict';

const leSdk = require('linkedge-core-sdk');
const iotData = new leSdk.IoTData();

exports.handler = function (event, context, callback) {
  var message = {
    topic: '/hello/world',
    payload: 'Hello World.',
  };
  iotData.publish(message, (err, data)=> {
    if (err == null) {
      console.log("-- Publish HelloWorld success.");
    }
    callback(err);
  });
};