Skip to content

Commit

Permalink
Implement socket.io room support in Sockend (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
orrgal1 authored and dashersw committed Jun 25, 2018
1 parent 01e0f10 commit 5ef7d38
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 85 deletions.
91 changes: 57 additions & 34 deletions README.md
Expand Up @@ -33,7 +33,7 @@ Develop your first microservices in under two minutes:
in `time-service.js`...
```js
const cote = require('cote');
const timeService = new cote.Responder({name: 'Time Service'});
const timeService = new cote.Responder({ name: 'Time Service' });

timeService.on('time', (req, cb) => {
cb(new Date());
Expand All @@ -43,9 +43,9 @@ timeService.on('time', (req, cb) => {
in `client.js`...
```js
const cote = require('cote');
const client = new cote.Requester({name: 'Client'});
const client = new cote.Requester({ name: 'Client' });

client.send({type: 'time'}, (time) => {
client.send({ type: 'time' }, (time) => {
console.log(time);
});
```
Expand Down Expand Up @@ -528,13 +528,13 @@ const randomRequester = new cote.Requester({
name: 'Random Requester',
// namespace: 'rnd',
// key: 'a certain key',
requests: ['randomRequest']
requests: ['randomRequest'],
});

setInterval(() => {
const req = {
type: 'randomRequest',
val: Math.floor(Math.random() * 10)
val: Math.floor(Math.random() * 10),
};

randomRequester.send(req, (res) => {
Expand All @@ -551,13 +551,13 @@ Example:

```js
const cote = require('cote');
const randomRequester = new cote.Requester({name: 'Random Requester'});
const randomRequester = new cote.Requester({ name: 'Random Requester' });

const makeRequest = (req) => randomRequester.send(req);

const req = {
type: 'randomRequest',
val: Math.floor(Math.random() * 10)
val: Math.floor(Math.random() * 10),
};

makeRequest(req)
Expand Down Expand Up @@ -590,7 +590,7 @@ const randomResponder = new cote.Responder({
name: 'Random Responder',
// namespace: 'rnd',
// key: 'a certain key',
respondsTo: ['randomRequest'] // types of requests this responder
respondsTo: ['randomRequest'], // types of requests this responder
// can respond to.
});

Expand All @@ -614,19 +614,19 @@ const cote = require('cote');
const UserModel = require('UserModel'); // a promise-based model API such as
// mongoose.

const userResponder = new cote.Responder({name: 'User Responder'});
const userResponder = new cote.Responder({ name: 'User Responder' });

userResponder.on('find', (req) => UserModel.findOne(req.query));
```

`requester.js`
```js
const cote = require('cote');
const userRequester = new cote.Requester({name: 'User Requester'});
const userRequester = new cote.Requester({ name: 'User Requester' });

userRequester
.send({type: 'find', query: {username: 'foo'}})
.then(user => console.log(user))
.send({ type: 'find', query: { username: 'foo' } })
.then((user) => console.log(user))
.then(process.exit);
```

Expand Down Expand Up @@ -656,13 +656,13 @@ const randomPublisher = new cote.Publisher({
name: 'Random Publisher',
// namespace: 'rnd',
// key: 'a certain key',
broadcasts: ['randomUpdate']
broadcasts: ['randomUpdate'],
});

// Wait for the publisher to find an open port and listen on it.
setInterval(function() {
const val = {
val: Math.floor(Math.random() * 1000)
val: Math.floor(Math.random() * 1000),
};

console.log('emitting', val);
Expand All @@ -685,7 +685,7 @@ const randomSubscriber = new cote.Subscriber({
name: 'Random Subscriber',
// namespace: 'rnd',
// key: 'a certain key',
subscribesTo: ['randomUpdate']
subscribesTo: ['randomUpdate'],
});

randomSubscriber.on('randomUpdate', (req) => {
Expand All @@ -707,25 +707,29 @@ Example:
```html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
var socketNamespaced = io.connect('/rnd');
let socket = io.connect();
let socketNamespaced = io.connect('/rnd');
socket.on('randomUpdate', function(data) {
console.log(data);
});
setInterval(function() {
var req = {
val: Math.floor(Math.random() * 10)
let req = {
val: Math.floor(Math.random() * 10),
};
var req2 = {
val: Math.floor(Math.random() * 10)
let req2 = {
val: Math.floor(Math.random() * 10),
};
var req3 = {
val: Math.floor(Math.random() * 10)
let req3 = {
val: Math.floor(Math.random() * 10),
};
var req4 = {
val: Math.floor(Math.random() * 10)
}
let req4 = {
val: Math.floor(Math.random() * 10),
};
socket.emit('randomRequest', req, function(data) {
console.log('normal', req.val, data);
Expand Down Expand Up @@ -753,6 +757,10 @@ const cote = require('cote'),
io = require('socket.io').listen(app),
fs = require('fs');

io.on('connection', (socket) => {
socket.join('room1');
});

app.listen(process.argv[2] || 5555);

function handler(req, res) {
Expand All @@ -772,11 +780,26 @@ const sockend = new cote.Sockend(io, {
// key: 'a certain key'
});
```

Now, fire up a few `Responder`s and `Publisher`s (from the `examples` folder)
on default or 'rnd' namespace and watch them glow with magic on
`http://localhost:5555`.

##### Socket.io Rooms
`Sockend` supports socket.io rooms. All you need to do is add a `__rooms` or `__room` attribute to
the published message.

```js
const randomPublisher = new cote.Publisher({
name: 'Random Publisher',
// namespace: 'rnd',
// key: 'a certain key',
broadcasts: ['randomUpdate'],
});

randomPublisher.publish('randomUpdate', { val: 500, __rooms: ['room1', 'room2'] });
randomPublisher.publish('randomUpdate', { val: 500, __room: 'room1' });
```

### Monitor

Monitor is the "top" of cote. It lists all the daemons it discovers regardless
Expand Down Expand Up @@ -883,12 +906,12 @@ const cote = require('cote');

const purchaseRequester = new cote.Requester({
name: 'Purchase Requester',
key: 'purchase'
key: 'purchase',
});

const inventoryRequester = new cote.Requester({
name: 'Inventory Requester',
key: 'inventory'
key: 'inventory',
});
```

Expand Down Expand Up @@ -921,12 +944,12 @@ const cote = require('cote');

const responder = new cote.Responder({
name: 'Conversion Sockend Responder',
namespace: 'conversion'
namespace: 'conversion',
});

const conversionRequester = new cote.Requester({
name: 'Conversion Requester',
key: 'conversion backend'
key: 'conversion backend',
});

responder.on('convert', (req, cb) => {
Expand All @@ -941,7 +964,7 @@ const cote = require('cote');

const responder = new cote.Responder({
name: 'Conversion Responder',
key: 'conversion backend'
key: 'conversion backend',
});

const rates = { usd_eur: 0.91, eur_usd: 1.10 };
Expand Down Expand Up @@ -1078,7 +1101,7 @@ accomodate different solutions that can serve as the automated service
discovery tool. Currently, redis is supported out of the box, and cote
makes use of the [node_redis](https://github.com/NodeRedis/node_redis)
library, in case you want to use redis as the central discovery tool. If you
need to use anything other than redis, please open
need to use anything other than redis, please open
[a new issue](https://github.com/dashersw/cote/issues/new) and we may be
able to help.

Expand All @@ -1092,7 +1115,7 @@ container deployment configurations such as Docker Swarm stack definitions
can make use of the additional redis backend functionality, while developers
can still use IP broadcast/multicast locally, with the same source code.

That's why cote uses environment variables that start with
That's why cote uses environment variables that start with
`COTE_DISCOVERY_REDIS`. cote transforms any environment variable that
starts with `COTE_DISCOVERY_REDIS` to proper configuration for the
[node_redis](https://github.com/NodeRedis/node_redis) library. For example,
Expand Down
5 changes: 3 additions & 2 deletions dist/components/publisher.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/components/publisher.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions dist/components/sockend.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ef7d38

Please sign in to comment.