evrythng-pubsub.js
Plugin for evrythng.js
(v5.1.0 and
above) allowing easy subscription and publication to resources such as Thngs,
products, and actions using either MQTT or WebSockets.
Install
npm
npm i --save evrythng-pubsub
Then require it into any module.
const evrythng = const PubSub = evrythng
CDN
Add it as a script tag to your page:
Then use in the same manner as for Node:
Usage
After installing the plugin with evrythng.use()
, three methods are added to
all resource types, such as Thngs, products, actions, etc. if they are
available as subscription topics.
.subscribe(onMessage)
- Subscribe to topic updates with a callback..unsubscribe()
- Unsubscribe from topic updates..publish(payload)
- Publish to a topic with payload data, such as an action.
Examples
Subscribe to all actions:
const onActionCreated = { console} await useraction'all'
Pubish a new action:
const payload = type: 'scans' thng: thngId await useraction'all'
Unsubscribe from all actions:
await useraction'all'
Testing
Use the tests/browser
and tests/node
directories to test this SDK in the
browser or Node, or run the Mocha test suite using a testable Trusted
Application API Key:
export TRUSTED_API_KEY=a87s9j3h...
npm test