Amplify-js: Pub-Sub from Lambda?

Created on 13 Sep 2018  路  2Comments  路  Source: aws-amplify/amplify-js

How would I publish a message to an Amplify client via a Lambda function creating an event in a IoT pub-sub topic? Does the Lambda func need to use Amplify or can you publish messages using the aws-sdk?

PubSub question

Most helpful comment

Thanks I'll give this a try. This should be better documented as I wasn't clear at all how to get started from the aws-sdk docs.

All 2 comments

Create the AWS IoT client using the aws-sdk

//import and prepare iot for pubsub
const IotData = require("aws-sdk/clients/iotdata");
let iotData;

export default function getIotData() {
  if (iotData) {
    return iotData;
  } else {
    iotData = new IotData({
      endpoint: process.env["AWS_IOT_ENDPOINT"],
      region: process.env["AWS_IOT_REGION"]
    });

    return iotData;
  }
}

And then publish like so:

import getIotData from "./getIotData";

export default async function publishDataObjToChannel(channelName, payload) {
  const iotData = getIotData();
  const topic = channelName;

  const iotParams = {
    payload: JSON.stringify(payload),
    topic,
    qos: 0
  };

  return await iotData.publish(iotParams).promise();
}

Thanks I'll give this a try. This should be better documented as I wasn't clear at all how to get started from the aws-sdk docs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leantide picture leantide  路  3Comments

karlmosenbacher picture karlmosenbacher  路  3Comments

cosmosof picture cosmosof  路  3Comments

TheRealRed7 picture TheRealRed7  路  3Comments

DougWoodCDS picture DougWoodCDS  路  3Comments