Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jimmyn/aws-mqtt-client
AWS Websocket Pub/Sub client
https://github.com/jimmyn/aws-mqtt-client
aws aws-iot aws-mqtt aws-websockets mqtt websockets
Last synced: 2 days ago
JSON representation
AWS Websocket Pub/Sub client
- Host: GitHub
- URL: https://github.com/jimmyn/aws-mqtt-client
- Owner: jimmyn
- License: other
- Created: 2016-07-11T13:53:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T07:02:43.000Z (almost 2 years ago)
- Last Synced: 2024-12-16T21:11:43.178Z (9 days ago)
- Topics: aws, aws-iot, aws-mqtt, aws-websockets, mqtt, websockets
- Language: JavaScript
- Homepage:
- Size: 265 KB
- Stars: 155
- Watchers: 6
- Forks: 17
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AWS Websocket Pub/Sub 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/whats-new/2016/01/aws-iot-now-supports-websockets-custom-keepalive-intervals-and-enhanced-console/) 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](https://aws.amazon.com/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: AWS_ACCESS_KEY,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
sessionToken: AWS_SESSION_TOKEN,
endpointAddress: AWS_IOT_ENDPOINT_HOST,
region: "us-east-1"
});
````
4. 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());
});
````
5. Publish a message.
````js
mqttClient.publish(MQTT_TOPIC, message);
````### Complete [MQTT.js API](https://github.com/mqttjs/MQTT.js#api)
## Credits
Based on [Serverless JS-Webapp Pub/Sub with AWS IoT](http://stesie.github.io/2016/04/aws-iot-pubsub) article by [Stefan Siegl](https://github.com/stesie). This library is a wrapper around [MQTT.js](https://github.com/mqttjs/MQTT.js) npm package.