socket.io-emitter
Advanced tools
Comparing version 0.2.0 to 0.3.0
0.3.0 / 2015-12-09 | ||
================== | ||
* make the module compatible with socket.io-redis `0.2.0` | ||
0.2.0 / 2014-06-07 | ||
@@ -3,0 +8,0 @@ ================== |
33
index.js
@@ -9,3 +9,3 @@ | ||
var hasBin = require('has-binary-data'); | ||
var msgpack = require('msgpack-js').encode; | ||
var msgpack = require('msgpack-js'); | ||
var debug = require('debug')('socket.io-emitter'); | ||
@@ -32,2 +32,10 @@ | ||
/** | ||
* uid for emitter | ||
* | ||
* @api private | ||
*/ | ||
var uid = 'emitter'; | ||
/** | ||
* Socket.IO redis based emitter. | ||
@@ -62,3 +70,3 @@ * | ||
this.redis = redis; | ||
this.key = (opts.key || 'socket.io') + '#emitter'; | ||
this.prefix = (opts.key || 'socket.io'); | ||
@@ -111,6 +119,8 @@ this._rooms = []; | ||
* | ||
* @api private | ||
* @api public | ||
*/ | ||
Emitter.prototype.emit = function(){ | ||
var self = this; | ||
// packet | ||
@@ -129,8 +139,19 @@ var args = Array.prototype.slice.call(arguments); | ||
// publish | ||
this.redis.publish(this.key, msgpack([packet, { | ||
var opts = { | ||
rooms: this._rooms, | ||
flags: this._flags | ||
}])); | ||
}; | ||
var chn = this.prefix + '#' + packet.nsp + '#'; | ||
var msg = msgpack.encode([uid, packet, opts]); | ||
// publish | ||
if (opts.rooms && opts.rooms.length) { | ||
opts.rooms.forEach(function(room) { | ||
var chnRoom = chn + room + '#'; | ||
self.redis.publish(chnRoom, msg); | ||
}); | ||
} else { | ||
this.redis.publish(chn, msg); | ||
} | ||
// reset state | ||
@@ -137,0 +158,0 @@ this._rooms = []; |
{ | ||
"name": "socket.io-emitter", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/socketio/socket.io-emitter.git" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"dependencies": { | ||
"debug": "0.7.4", | ||
"redis": "0.10.1", | ||
"debug": "2.2.0", | ||
"redis": "2.4.2", | ||
"msgpack-js": "0.3.0", | ||
"has-binary-data": "0.1.1", | ||
"socket.io-parser": "2.2.0" | ||
"has-binary-data": "0.1.5", | ||
"socket.io-parser": "2.2.6" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1.18.1", | ||
"mocha": "2.3.4", | ||
"expect.js": "0.3.1", | ||
"socket.io": "1.0.4", | ||
"socket.io-client": "1.0.4", | ||
"socket.io-redis": "0.1.3", | ||
"redis": "0.10.1" | ||
"socket.io": "1.3.7", | ||
"socket.io-client": "1.3.7", | ||
"socket.io-redis": "0.2.0" | ||
} | ||
} |
# socket.io-emitter | ||
[![Build Status](https://travis-ci.org/socketio/socket.io-emitter.svg?branch=master)](https://travis-ci.org/socketio/socket.io-emitter) | ||
[![NPM version](https://badge.fury.io/js/socket.io-emitter.svg)](http://badge.fury.io/js/socket.io-emitter) | ||
`socket.io-emitter` allows you to communicate with socket.io servers | ||
@@ -10,3 +13,3 @@ easily from non-socket.io processes. | ||
```js | ||
var io = require('socket.io-emitter')(); | ||
var io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 }); | ||
setInterval(function(){ | ||
@@ -13,0 +16,0 @@ io.emit('time', new Date); |
@@ -16,3 +16,3 @@ var expect = require('expect.js'); | ||
if (!addr) addr = srv.listen().address(); | ||
var url = 'ws://' + addr.address + ':' + addr.port + (nsp || ''); | ||
var url = 'http://localhost:' + addr.port + (nsp || ''); | ||
return ioc(url, opts); | ||
@@ -23,14 +23,15 @@ } | ||
var srv; | ||
describe('in namespaces', function(){ | ||
beforeEach(function() { | ||
var pub = redis.createClient(); | ||
var sub = redis.createClient(null, null, {return_buffers: true}); | ||
srv = http(); | ||
var sio = io(srv, {adapter: redisAdapter({pubClient: pub, subClient: sub})}); | ||
beforeEach(function() { | ||
var pub = redis.createClient(); | ||
var sub = redis.createClient(null, null, {detect_buffers: true}); | ||
srv = http(); | ||
var sio = io(srv, {adapter: redisAdapter({pubClient: pub, subClient: sub})}); | ||
srv.listen(function() { | ||
['/', '/nsp'].forEach(function(nsp) { | ||
sio.of(nsp).on('connection', function(socket) { | ||
socket.on('broadcast event', function(payload) { | ||
socket.emit('broadcast event', payload); | ||
srv.listen(function() { | ||
['/', '/nsp'].forEach(function(nsp) { | ||
sio.of(nsp).on('connection', function(socket) { | ||
socket.on('broadcast event', function(payload) { | ||
socket.emit('broadcast event', payload); | ||
}); | ||
}); | ||
@@ -40,50 +41,137 @@ }); | ||
}); | ||
}); | ||
it('should be able to emit messages to client', function(done) { | ||
var emitter = ioe({ host: 'localhost', port: '6379' }); | ||
var cli = client(srv, { forceNew: true }); | ||
cli.on('connect', function() { | ||
emitter.emit('broadcast event', 'broadacast payload'); | ||
}); | ||
it('should be able to emit messages to client', function(done) { | ||
var emitter = ioe({ host: 'localhost', port: '6379' }); | ||
var cli = client(srv, { forceNew: true }); | ||
cli.on('connect', function() { | ||
emitter.emit('broadcast event', 'broadacast payload'); | ||
}); | ||
cli.on('broadcast event', function(payload) { | ||
cli.close(); | ||
done(); | ||
cli.on('broadcast event', function(payload) { | ||
cli.close(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should be able to emit message to namespace', function(done) { | ||
var cli = client(srv, '/nsp', { forceNew: true }); | ||
cli.on('broadcast event', function(payload) { | ||
cli.close(); | ||
done(); | ||
it('should be able to emit message to namespace', function(done) { | ||
var cli = client(srv, '/nsp', { forceNew: true }); | ||
cli.on('broadcast event', function(payload) { | ||
cli.close(); | ||
done(); | ||
}); | ||
cli.on('connect', function() { | ||
var emitter = ioe({ host: 'localhost', port: '6379' }); | ||
emitter.of('/nsp').broadcast.emit('broadcast event', 'broadcast payload'); | ||
}); | ||
}); | ||
cli.on('connect', function() { | ||
var emitter = ioe({ host: 'localhost', port: '6379' }); | ||
emitter.of('/nsp').broadcast.emit('broadcast event', 'broadcast payload'); | ||
it('should not emit message to all namespaces', function(done) { | ||
var a = client(srv, '/nsp', { forceNew: true }); | ||
var b; | ||
a.on('connect', function() { | ||
b = client(srv, { forceNew: true }); | ||
b.on('broadcast event', function(payload) { | ||
expect().fail(); | ||
}); | ||
b.on('connect', function() { | ||
var emitter = ioe({ host: 'localhost', port: '6379' }); | ||
emitter.of('/nsp').broadcast.emit('broadcast event', 'broadcast payload'); | ||
}); | ||
}); | ||
a.on('broadcast event', function(payload) { | ||
setTimeout(done, 1000); | ||
}); | ||
}); | ||
}); | ||
it('should not emit message to all namespaces', function(done) { | ||
var a = client(srv, '/nsp', { forceNew: true }); | ||
var b; | ||
describe('in rooms', function(){ | ||
it('should be able to emit to a room', function(done){ | ||
var pub = redis.createClient(); | ||
var sub = redis.createClient(null, null, {return_buffers: true}); | ||
srv = http(); | ||
var sio = io(srv, {adapter: redisAdapter({pubClient: pub, subClient: sub})}); | ||
a.on('connect', function() { | ||
b = client(srv, { forceNew: true }); | ||
b.on('broadcast event', function(payload) { | ||
var secondConnecting = false; | ||
srv.listen(function() { | ||
sio.on('connection', function(socket) { | ||
if (secondConnecting) { | ||
socket.join('exclusive room'); | ||
} else { | ||
secondConnecting = true; | ||
} | ||
socket.on('broadcast event', function(payload) { | ||
socket.emit('broadcast event', payload); | ||
}); | ||
}); | ||
}); | ||
var a = client(srv, { forceNew: true }); | ||
a.on('broadcast event', function(payload) { | ||
expect().fail(); | ||
}); | ||
b.on('connect', function() { | ||
var emitter = ioe({ host: 'localhost', port: '6379' }); | ||
emitter.of('/nsp').broadcast.emit('broadcast event', 'broadcast payload'); | ||
var b; | ||
a.on('connect', function() { | ||
b = client(srv, { forceNew: true }); | ||
b.on('broadcast event', function(payload) { | ||
expect(payload).to.be('broadcast payload'); | ||
setTimeout(done, 1000); | ||
}); | ||
b.on('connect', function() { | ||
var emitter = ioe({ host: 'localhost', port: '6379' }); | ||
emitter.to('exclusive room').broadcast.emit('broadcast event', 'broadcast payload'); | ||
}); | ||
}); | ||
}); | ||
a.on('broadcast event', function(payload) { | ||
setTimeout(done, 1000); | ||
it('should be able to emit to a socket by id', function(done){ | ||
var pub = redis.createClient(); | ||
var sub = redis.createClient(null, null, {return_buffers: true}); | ||
srv = http(); | ||
var sio = io(srv, {adapter: redisAdapter({pubClient: pub, subClient: sub})}); | ||
var secondConnecting = false; | ||
var secondId; | ||
srv.listen(function() { | ||
sio.on('connection', function(socket) { | ||
if (secondConnecting) { | ||
secondId = socket.id; | ||
} else { | ||
secondConnecting = true; | ||
} | ||
socket.on('broadcast event', function(payload) { | ||
socket.emit('broadcast event', payload); | ||
}); | ||
}); | ||
}); | ||
var a = client(srv, { forceNew: true }); | ||
a.on('broadcast event', function(payload) { | ||
expect().fail(); | ||
}); | ||
var b; | ||
a.on('connect', function() { | ||
b = client(srv, { forceNew: true }); | ||
b.on('broadcast event', function(payload) { | ||
expect(payload).to.be('broadcast payload'); | ||
setTimeout(done, 1000); | ||
}); | ||
b.on('connect', function() { | ||
var emitter = ioe({ host: 'localhost', port: '6379' }); | ||
emitter.to(secondId).broadcast.emit('broadcast event', 'broadcast payload'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
11013
5
290
58
0
+ Addedbenchmark@1.0.0(transitive)
+ Addedcomponent-emitter@1.1.2(transitive)
+ Addeddebug@2.2.0(transitive)
+ Addeddouble-ended-queue@2.1.0-0(transitive)
+ Addedhas-binary-data@0.1.5(transitive)
+ Addedjson3@3.3.2(transitive)
+ Addedms@0.7.1(transitive)
+ Addedredis@2.4.2(transitive)
+ Addedredis-commands@1.7.0(transitive)
+ Addedsocket.io-parser@2.2.6(transitive)
- Removeddebug@0.7.4(transitive)
- Removedhas-binary-data@0.1.1(transitive)
- Removedjson3@3.2.6(transitive)
- Removedredis@0.10.1(transitive)
- Removedsocket.io-parser@2.2.0(transitive)
Updateddebug@2.2.0
Updatedhas-binary-data@0.1.5
Updatedredis@2.4.2
Updatedsocket.io-parser@2.2.6