Skip to content

Commit

Permalink
Merge pull request #9 from evrythng/v1-dev
Browse files Browse the repository at this point in the history
Update for evrythng.js v5, to replace separate MQTT and WS SDKs
  • Loading branch information
Ricardo Espirito Santo authored Jun 11, 2019
2 parents 49fb22d + 4bab290 commit cf3ce40
Show file tree
Hide file tree
Showing 32 changed files with 9,657 additions and 194 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ bower_components
dist
report
.idea
*.sh
2 changes: 0 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ node_modules
bower_components
report
.idea
docker
Dockerfile
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:8-alpine

WORKDIR /srv
COPY . /srv

# Install dependencues
RUN apk add --no-cache python3
RUN pip3 install awscli --upgrade --user
RUN npm i

# Build
RUN npm run build

# Deploy
CMD ["sh", "-c", "~/.local/bin/aws s3 cp /srv/dist/evrythng-pubsub.js s3://$BUCKET/js/evrythng-pubsub/$VERSION/evrythng-pubsub-$VERSION.js --acl public-read"]
100 changes: 64 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,91 @@
# evrythng-pubsub.js [![Build Status](https://travis-ci.org/evrythng/evrythng-pubsub.js.svg?branch=master)](https://travis-ci.org/evrythng/evrythng-pubsub.js)
# evrythng-pubsub.js

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](http://standardjs.com)
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.

## Getting started

### Install evrythng-pubsub.js using npm.
## Install

```javascript
npm install evrythng-pubsub
### npm

```
npm i --save evrythng-pubsub
```

Then require it into any module.

```javascript
const EVT = require('evrythng')
const EVTPubSub = require('evrythng-pubsub')
```js
const evrythng = require('evrythng')
const PubSub = require('evrythng-pubsub')

evrythng.use(PubSub)
```

EVT.use(EVTPubSub)
### CDN

/* ... Init app using EVT.js ... */
Add it as a script tag to your page:

app.scan().then(match => {
app.redirect(match.redirections[0].redirectUrl)
})
```html
<script src="https://d10ka0m22z5ju5.cloudfront.net/js/evrythng-pubsub/1.0.0/evrythng-pubsub-1.0.0.js"></script>
```

### Browser

To use immutable from a browser, download `dist/evrythng-pubsub.min.js` or use a CDN such as CDNJS or jsDelivr.
Then use in the same manner as for Node:

Then, add it as a script tag to your page:

```html
<script src="evrythng.min.js"></script>
<script src="evrythng-pubsub.min.js"></script>
<script>
EVT.use(EVTPubSub)
evrythng.use(PubSub)
</script>
```

/* ... Init app using EVT.js ... */

app.scan().then(match => {
app.redirect(match.redirections[0].redirectUrl)
})
</script>
## 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)
```

Or use an AMD loader (such as RequireJS):
Pubish a new action:

```js
const payload = { type: 'scans', thng: thngId }

```javascript
require(['./evrythng.min.js', './evrythng-pubsub.min.js'], (EVT, EVTPubSub) => {
EVT.use(EVTPubSub)
await user.action('all').publish(payload)
```

/* ... Init app using EVT.js ... */
Unsubscribe from all actions:

app.scan().then(match => {
app.redirect(match.redirections[0].redirectUrl)
})
})
```js
await user.action('all').unsubscribe()
```

If you're using browserify, the `evrythng-pubsub npm module also works from the browser.

## 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
```
39 changes: 0 additions & 39 deletions bower.json

This file was deleted.

7 changes: 0 additions & 7 deletions evrythng.config.js

This file was deleted.

2 changes: 0 additions & 2 deletions gulpfile.js

This file was deleted.

19 changes: 19 additions & 0 deletions jenkins/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Use a Docker container to build and deploy a new SDK build to the S3 bucket
# used for distribution (publish to npm happens separately).
#
# Required environment variables:
#  BUCKET - The S3 bucket to push the built SDK file.
#  VERSION - The new SDK version being published.
#  AWS_ACCESS_KEY_ID - AWS Access Key ID with permission to put to the bucket.
#  AWS_SECRET_ACCESS_KEY - AWS Secret Key ID corresponding to the AWS_ACCESS_KEY_ID.

docker build -t evrythng-pubsub-js-deploy .

docker run \
-e "BUCKET=$BUCKET" \
-e "VERSION=$VERSION" \
-e "AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID" \
-e "AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY" \
evrythng-pubsub-js-deploy
Loading

0 comments on commit cf3ce40

Please sign in to comment.