README
DropPay SensorJS
A driver oriented realtime communication library.
Installation
NodeJS
npm command
Using npm install droppay-sensor-js -S
plain
javascript on browsers
Using <script type="application/ecmascript" src="dist/sensor.js">
Supported Drivers
API
- connect
- disconnect
- subscribe
- unsubscribe
- addListener
- removeListener
- release
- publish
connect()
Initialize a connection with the service provider.
disconnect()
Close connection with the service provider.
subscribe(ChannelId: String/Array)
Subscribe to a specific channel. Depending on the driver an array of channels can be used.
unsubscribe(ChannelId: String/Array)
Unsubscribe from a specific channel. Depending on the driver an array of channels can be used.
addListener(callback(id: int, message: Object): Function)
Add a listener to the messages queue. Anytime a message is received on the subscribed channel(s) through the estabilished connection, all listeners are notified.
removeListener(callback: Function)
Wipe off a listener from the queue.
release()
Release all channels and disconnect from service provider endpoint.
publish(name/channel: String, payload: String/Object, callback: Function)
Send a payload to the subscribed channel through the estabilished connection. Name/Channel are mandatory, depending on the driver used they can be a message NAME.
or a CHANNEL_ID
.
Example (PubNub)
import DropPaySensorJS from 'droppay-sensor-js';
const sensor = DropPaySensorJS('pubnub', {
subscribeKey: 'sub-xxx',
publishKey: 'pub-xxx',
ssl: true
});
sensor.addListener((message)=>{
console.log('received message: ' + JSON.stringify(message))
});
sensor.subscribe('myChannel');
sensor.publish('myChannel', (status, response)=>{
if (status.error) {
// handle error
console.log(status)
} else {
console.log("message Published w/ timetoken", response.timetoken)
}
});
sensor.unsubscribe('myChannel');
sensor.release();