Skip to content

Commit 47a373c

Browse files
committed
Add listening and forwarding
1 parent 541d1d3 commit 47a373c

File tree

7 files changed

+46
-27
lines changed

7 files changed

+46
-27
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
"consistent-return" : "warn",
1919
"curly" : ["error", "multi-or-nest"],
2020
"no-plusplus" : "off",
21+
"no-console" : "off"
2122
}
2223
}

README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ events from the public internet to a local network.
1717

1818
To use the `ubsub` CLI on your command line, make sure you have a recent version of [NodeJS](https://nodejs.org/en/) installed, and then run:
1919

20-
```
20+
```bash
2121
npm install -g ubsub
2222
```
2323

@@ -27,17 +27,29 @@ Once installed, you can run `ubsub help` from command line to see help output.
2727

2828
Installing into your project:
2929

30-
```
30+
```bash
3131
npm install --save ubsub-client
3232
```
3333

34+
See [examples/](examples/) for some sample uses.
35+
3436
### Listening to a Topic
3537

36-
TODO
38+
```js
39+
const ubsub = require('../')(<user id>, <user secret>);
40+
41+
ubsub.listen(<topic id>, (event) => {
42+
console.log('received event ' + JSON.stringify(event));
43+
});
44+
```
3745

3846
### Forwarding a Topic to an HTTP endpoint
3947

40-
TODO
48+
```js
49+
const ubsub = require('../')(<user id>, <user secret>);
50+
51+
ubsub.listen(<topic id>, 'http://localhost:5000', {..optional axios opts..});
52+
```
4153

4254
# License
4355

cli.js

100644100755
File mode changed.

examples/listening.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env node
2+
const ubsub = require('../')('ry2VZ8e3Z', 'a12e59acf83bbda211e02f9f7bbe6069eeac623412953da8b8179ef4db4ee061');
3+
4+
/* eslint no-console: off */
5+
ubsub.listen('SylW8bIe3Z', (event) => {
6+
console.log(`received event ${JSON.stringify(event)}`);
7+
});
8+
9+
ubsub.forward('SylW8bIe3Z', 'http://localhost:8000');

index.js

+20-23
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,28 @@ const io = require('socket.io-client');
33
const axios = require('axios');
44
const _ = require('lodash');
55

6-
function UbSubClient(userId, userKey, opts) {
6+
module.exports = (userId, userKey, opts) => {
77
const o = opts || {};
8+
const host = o.host || 'https://socket.ubsub.io';
89

9-
this._host = o.host || 'https://socket.ubsub.io';
10-
this._userId = userId;
11-
this._userSecret = userKey;
10+
return {
11+
listen(topicId, onEvent) {
12+
const sock = io(`${host}/socket?userId=${userId}&topicId=${topicId}&userKey=${userKey}`);
13+
sock.on('event', onEvent);
14+
return sock;
15+
},
1216

13-
this._sockets = {};
14-
}
17+
forward(topicId, forwardUrl, httpOpts) {
18+
return this.listen(topicId, data => {
19+
axios(_.assign({
20+
url: forwardUrl,
21+
data,
22+
method: 'post', // default, can be overriden in opts
23+
}, httpOpts)).catch(err => {
24+
console.error(`Error forwarding event from '${topicId} to URL ${forwardUrl}: ${err.message}`);
25+
});
26+
});
27+
},
1528

16-
UbSubClient.prototype.listen = function listen(topicId, onEvent) {
17-
const sock = io(`${this._host}/socket?userId=${this._userId}&topicId=${topicId}&userKey=${this._userKey}`);
18-
sock.on('event', onEvent);
29+
};
1930
};
20-
21-
UbSubClient.prototype.forward = function forward(topicId, forwardUrl, opts) {
22-
this.listen(topicId, data => {
23-
axios(_.assign({
24-
url: forwardUrl,
25-
data,
26-
method: 'post', // default, can be overriden in opts
27-
}, opts));
28-
});
29-
};
30-
31-
32-
module.exports = new UbSubClient();
33-
module.exports.Client = UbSubClient;

lib/client.js

Whitespace-only changes.

lib/listener.js

Whitespace-only changes.

0 commit comments

Comments
 (0)