The aws-iot-mqtt-websocket-sdk-js library is developed to write JavaScript applications for browser which access the AWS IoT Platform via MQTT over the Secure WebSocket Protocol.
This library has following dependencies:
Installing with bower:
bower install aws-iot-mqtt-websocket-sdk-js --save
Installing from github:
git clone https://github.com/tapasbose/aws-iot-mqtt-websocket-sdk-js.git
cd aws-iot-mqtt-websocket-sdk-js
bower install
The class Thing
represents an Internet Of Thing connected with AWS IoT via MQTT Protocol over Secure WebSocket. To instantiate this class you need to do the following:
var thing = new Thing({
regionName: 'your_region',
endpoint: 'your_aws_iot_endpoint',
accessKey: 'your_access_key',
secretKey: 'your_secret_key',
thingName: 'your_thing_name'
});
Once you have instantiate the class, you need to call register
method to connect your thing with AWS IoT via MQTT Protocol over Secure WebSocket.
A call to the method register
connects your thing with AWS IoT. After the connetion is successfull the thing subscribes to the following topics to get notifications:
- $aws/things/your_thing/shadow/get/+
- $aws/things/your_thing/shadow/update/+
A call to the method unRegister
disconnects your thing with AWS IoT and unregisters itself from the subscribed topics.
A call to the method get
is used to extract the current state information of your thing from AWS IoT. The state is returned by the channel /get/accepted. The event handler for the event thing.get.message.accepted
will receive this state.
This method takes an optional clientToken
parameter. If this parameter is not provided then the API will provide one for you. This clientToken
enables you to associate responses with requests in MQTT environment.
This method returns clientToken
.
A call to the method update
is used to publish state information of your thing to AWS IoT. If the update request is accecpted by AWS IoT then the updated state is returned through the channel update/accepted. The event handler for the event thing.update.message.accepted
will receive this state.
This method takes a mandetory stateObject
parameter and an optional clientToken
parameter. If the parameter clientToken
is not provided then the API will provide one for you. This clientToken
enables you to associate responses with requests in MQTT environment.
This method returns clientToken
(If stateObject
is undefined then it returns null).
A call to this method returns if the underlying mqtt client is connected or not.
This method returns the name of the thing associated with it.
thing.connection.success
: This event will be fired when the thing get connected successfully.thing.connection.fail
: This event will be fired when the thing fail to connect.thing.subscribe.success
: This event will be fired when the thing has successfully subscribed to a topic. Two objects will be passed to the callback handler:- topic: the topic in which the thing has successfully subscribed to.
- response: the raw response from the underlying mqtt client.
thing.subscribe.fail
: This event will be fired when the thing has failed to subscribe to a topic. Two objects will be passed to the callback handler:- topic: the topic in which the thing has failed to subscribe.
- response: the raw response from the underlying mqtt client.
thing.unsubscribe.success
: This event will be fired when the thing has successfully unsubscribed to a topic. Two objects will be passed to the callback handler:- topic: the topic in which the thing has successfully unsubscribed to.
- response: the raw response from the underlying mqtt client.
thing.unsubscribe.fail
: This event will be fired when the thing has failed to unsubscribe to a topic. Two objects will be passed to the callback handler:- topic: the topic in which the thing has failed to unsubscribe.
- response: the raw response from the underlying mqtt client.
thing.get.message.accepted
: This event will be fired when there is any message arrived in the channel /get/accepted. A JSON object will be passed to callback handler.thing.get.message.rejected
: This event will be fired when there is any message arrived in the channel /get/rejected. A JSON object will be passed to callback handler.thing.update.message.accepted
: This event will be fired when there is any message arrived in the channel /update/accepted. A JSON object will be passed to callback handler.thing.update.message.rejected
: This event will be fired when there is any message arrived in the channel /update/rejected. A JSON object will be passed to callback handler.thing.update.message.documents
: This event will be fired when there is any message arrived in the channel /update/documents. A JSON object will be passed to callback handler.thing.update.message.delta
: This event will be fired when there is any message arrived in the channel /update/delta. A JSON object will be passed to callback handler.
See sample directory for example.
aws-iot-mqtt-websocket-sdk-js is freely distributable under the terms of the MIT license.