aws-mqtt-client

[AWS MQTT](http://docs.aws.amazon.com/iot/latest/developerguide/protocols.html) Websocket Pub/Sub with AWS IoT based on [MQTT.js](https://github.com/mqttjs/MQTT.js). Recently [AWS released support of WebSockets for IoT](https://aws.amazon.com/about-aws/wh

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
aws-mqtt-client
15640.0.68 months ago7 years agoMinified + gzip package size for aws-mqtt-client in KB

Readme

AWS Websocket Pub/Sub client
AWS MQTT Websocket Pub/Sub with AWS IoT based on MQTT.js. Recently AWS released support of WebSockets for IoT service. It is very easy to use as Pub/Sub message system for serverless web applications. You can post new messages from AWS lambda function via http post request and receive them as websocket messages on client.

Installing it

````bash npm i aws-mqtt-client --save ````

Basic usage

  1. Create an IAM role and asign predefined AWSIoTDataAccess policy. (It is better to use AWS Cognito to provide temporary credentials for the front-end application, you can also customize policy to allow access only to user specific topics).
  2. Run AWS CLI command aws iot describe-endpoint to get IoT endpoint url.
  3. Create mqttClient with AWS credentials.
````js import AWSMqtt from "aws-mqtt-client";
const mqttClient = new AWSMqtt({ accessKeyId: AWSACCESSKEY, secretAccessKey: AWSSECRETACCESSKEY, sessionToken: AWSSESSIONTOKEN, endpointAddress: AWSIOTENDPOINTHOST, region: "us-east-1" }); ````
  1. Connect and receive messages from your topic.
````js mqttClient.on("connect", () => { mqttClient.subscribe("test-topic"); console.log("connected to iot mqtt websocket"); }); mqttClient.on("message", (topic, message) => { console.log(message.toString()); }); ````
  1. Publish a message.
````js mqttClient.publish(MQTTTOPIC, message); ````

Complete MQTT.js API

Credits

Based on Serverless JS-Webapp Pub/Sub with AWS IoT
article by Stefan Siegl. This library is a wrapper around MQTT.js npm package.