Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evrythng/evrythng-pubsub.js
Plugin for evrythng.js that adds PubSub connectivity with MQTT and MQTT over WebSockets.
https://github.com/evrythng/evrythng-pubsub.js
evrythng internetofthings iot mqtt pubsub websockets
Last synced: 24 days ago
JSON representation
Plugin for evrythng.js that adds PubSub connectivity with MQTT and MQTT over WebSockets.
- Host: GitHub
- URL: https://github.com/evrythng/evrythng-pubsub.js
- Owner: evrythng
- Created: 2016-12-16T10:32:32.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-10-20T09:36:02.000Z (about 3 years ago)
- Last Synced: 2024-09-29T08:41:01.614Z (about 1 month ago)
- Topics: evrythng, internetofthings, iot, mqtt, pubsub, websockets
- Language: JavaScript
- Size: 297 KB
- Stars: 2
- Watchers: 36
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# evrythng-pubsub.js
Plugin for [`evrythng.js`](https://github.com/evrythng/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.
```js
const evrythng = require('evrythng')
const PubSub = require('evrythng-pubsub')evrythng.use(PubSub)
```### CDN
Add it as a script tag to your page:
```html
```
Then use in the same manner as for Node:
```html
evrythng.use(PubSub)
```
## 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](https://developers.evrythng.com/docs/pubsub#section-available-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:
```js
const onActionCreated = (action) => {
console.log(`Action created: ${action.id} of type ${action.type}`)
}await user.action('all').subscribe(onActionCreated)
```Pubish a new action:
```js
const payload = { type: 'scans', thng: thngId }await user.action('all').publish(payload)
```Unsubscribe from all actions:
```js
await user.action('all').unsubscribe()
```## 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
```