@mhlabs/aws-iot-ws-broker

A broker for client side websocket pub/sub using AWS IoT

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@mhlabs/aws-iot-ws-broker
144.0.23 years ago6 years agoMinified + gzip package size for @mhlabs/aws-iot-ws-broker in KB

Readme

aws-iot-mqtt-broker

Usage:

Installation

npm i @mhlabs/aws-iot-ws-broker

Connecting (TypeScript)

import AwsIot, { IotEvent } from '@mhlabs/aws-iot-ws-broker';

// credentials could be e.g. from Cognito Identity Pool
const creds: CognitoIdentityCredentials = createCredentials();
this.websocket = new AwsIot(creds, !environment.production);

Subscribe to topic:

New in this version is that subscribe() returns an Observable and therefor needs to be subscribed to. The Observable with emit and complete when the topic has been subscribed to.
this.websocket.subscribe('your/topic').subscribe(topic => console.log(`${topic} has been subscribed to`));

Send message

this.websocket.send('your/topic', message);

Handle message:

this.websocket.events.pipe(filter(event => event.type === IotEventType.Message)).subscribe(event => console.log(`Message on topic ${event.topic}:`, event.message));