Skip to content

Commit e656600

Browse files
committed
make the module compatible with socket.io-redis 0.2.0
1 parent 230a3c4 commit e656600

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
var client = require('redis').createClient;
77
var parser = require('socket.io-parser');
88
var hasBin = require('has-binary-data');
9-
var msgpack = require('msgpack-js').encode;
9+
var msgpack = require('msgpack-js');
1010
var debug = require('debug')('socket.io-emitter');
1111

1212
/**
@@ -27,6 +27,14 @@ var flags = [
2727
'broadcast'
2828
];
2929

30+
/**
31+
* uid for emitter
32+
*
33+
* @api private
34+
*/
35+
36+
var uid = 'emitter';
37+
3038
/**
3139
* Socket.IO redis based emitter.
3240
*
@@ -57,7 +65,7 @@ function Emitter(redis, opts){
5765
}
5866

5967
this.redis = redis;
60-
this.key = (opts.key || 'socket.io') + '#emitter';
68+
this.prefix = (opts.key || 'socket.io');
6169

6270
this._rooms = [];
6371
this._flags = {};
@@ -105,10 +113,12 @@ Emitter.prototype.of = function(nsp) {
105113
/**
106114
* Send the packet.
107115
*
108-
* @api private
116+
* @api public
109117
*/
110118

111119
Emitter.prototype.emit = function(){
120+
var self = this;
121+
112122
// packet
113123
var args = Array.prototype.slice.call(arguments);
114124
var packet = {};
@@ -122,11 +132,22 @@ Emitter.prototype.emit = function(){
122132
packet.nsp = '/';
123133
}
124134

125-
// publish
126-
this.redis.publish(this.key, msgpack([packet, {
135+
var opts = {
127136
rooms: this._rooms,
128137
flags: this._flags
129-
}]));
138+
};
139+
var chn = this.prefix + '#' + packet.nsp + '#';
140+
var msg = msgpack.encode([uid, packet, opts]);
141+
142+
// publish
143+
if (opts.rooms && opts.rooms.length) {
144+
opts.rooms.forEach(function(room) {
145+
var chnRoom = chn + room + '#';
146+
self.redis.publish(chnRoom, msg);
147+
});
148+
} else {
149+
this.redis.publish(chn, msg);
150+
}
130151

131152
// reset state
132153
this._rooms = [];

package.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
"type": "git",
77
"url": "https://github.com/Automattic/socket.io-emitter.git"
88
},
9+
"scripts": {
10+
"test": "make test"
11+
},
912
"dependencies": {
10-
"debug": "0.7.4",
11-
"redis": "0.10.1",
13+
"debug": "2.2.0",
14+
"redis": "2.4.2",
1215
"msgpack-js": "0.3.0",
13-
"has-binary-data": "0.1.1",
14-
"socket.io-parser": "2.2.0"
16+
"has-binary-data": "0.1.5",
17+
"socket.io-parser": "2.2.6"
1518
},
1619
"devDependencies": {
17-
"mocha": "1.18.1",
20+
"mocha": "2.3.4",
1821
"expect.js": "0.3.1",
19-
"socket.io": "1.0.4",
20-
"socket.io-client": "1.0.4",
21-
"socket.io-redis": "0.1.3",
22-
"redis": "0.10.1"
22+
"socket.io": "1.3.7",
23+
"socket.io-client": "1.3.7",
24+
"socket.io-redis": "0.2.0"
2325
}
2426
}

test/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function client(srv, nsp, opts){
1313
}
1414
var addr = srv.address();
1515
if (!addr) addr = srv.listen().address();
16-
var url = 'ws://' + addr.address + ':' + addr.port + (nsp || '');
16+
var url = 'http://localhost:' + addr.port + (nsp || '');
1717
return ioc(url, opts);
1818
}
1919

@@ -22,7 +22,7 @@ describe('emitter', function() {
2222
describe('in namespaces', function(){
2323
beforeEach(function() {
2424
var pub = redis.createClient();
25-
var sub = redis.createClient(null, null, {detect_buffers: true});
25+
var sub = redis.createClient(null, null, {return_buffers: true});
2626
srv = http();
2727
var sio = io(srv, {adapter: redisAdapter({pubClient: pub, subClient: sub})});
2828

@@ -88,7 +88,7 @@ describe('emitter', function() {
8888
describe('in rooms', function(){
8989
it('should be able to emit to a room', function(done){
9090
var pub = redis.createClient();
91-
var sub = redis.createClient(null, null, {detect_buffers: true});
91+
var sub = redis.createClient(null, null, {return_buffers: true});
9292
srv = http();
9393
var sio = io(srv, {adapter: redisAdapter({pubClient: pub, subClient: sub})});
9494

@@ -130,7 +130,7 @@ describe('emitter', function() {
130130

131131
it('should be able to emit to a socket by id', function(done){
132132
var pub = redis.createClient();
133-
var sub = redis.createClient(null, null, {detect_buffers: true});
133+
var sub = redis.createClient(null, null, {return_buffers: true});
134134
srv = http();
135135
var sio = io(srv, {adapter: redisAdapter({pubClient: pub, subClient: sub})});
136136

0 commit comments

Comments
 (0)