forked from socketio/socket.io-redis-emitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (124 loc) · 2.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Module dependencies.
*/
var client = require('redis').createClient;
var parser = require('socket.io-parser');
var hasBin = require('has-binary-data');
var msgpack = require('msgpack-js').encode;
var debug = require('debug')('socket.io-emitter');
/**
* Module exports.
*/
module.exports = Emitter;
/**
* Flags.
*
* @api public
*/
var flags = [
'json',
'volatile',
'broadcast'
];
/**
* Socket.IO redis based emitter.
*
* @param {Object} redis client (optional)
* @param {Object} options
* @api public
*/
function Emitter(redis, opts){
if (!(this instanceof Emitter)) return new Emitter(redis, opts);
opts = opts || {};
if ('string' == typeof redis) {
redis = clientUri(redis);
}
if (redis && !redis.hset) {
opts = redis;
redis = null;
}
if (!redis) {
if (!opts.socket && !opts.host) throw new Error('Missing redis `host`');
if (!opts.socket && !opts.port) throw new Error('Missing redis `port`');
redis = opts.socket
? client(opts.socket)
: client(opts.port, opts.host);
}
this.redis = redis;
this.key = (opts.key || 'socket.io') + '#emitter';
this._rooms = [];
this._flags = {};
}
/**
* Apply flags from `Socket`.
*/
flags.forEach(function(flag){
Emitter.prototype.__defineGetter__(flag, function(){
debug('flag %s on', flag);
this._flags[flag] = true;
return this;
});
});
/**
* Limit emission to a certain `room`.
*
* @param {String} room
*/
Emitter.prototype.in =
Emitter.prototype.to = function(room){
if (!~this._rooms.indexOf(room)) {
debug('room %s', room);
this._rooms.push(room);
}
return this;
};
/**
* Limit emission to certain `namespace`.
*
* @param {String} namespace
*/
Emitter.prototype.of = function(nsp) {
debug('nsp set to %s', nsp);
this._flags.nsp = nsp;
return this;
};
/**
* Send the packet.
*
* @api private
*/
Emitter.prototype.emit = function(){
// packet
var args = Array.prototype.slice.call(arguments);
var packet = {};
packet.type = hasBin(args) ? parser.BINARY_EVENT : parser.EVENT;
packet.data = args;
// set namespace to packet
if (this._flags.nsp) {
packet.nsp = this._flags.nsp;
delete this._flags.nsp;
} else {
packet.nsp = '/';
}
// publish
this.redis.publish(this.key, msgpack([packet, {
rooms: this._rooms,
flags: this._flags
}]));
// reset state
this._rooms = [];
this._flags = {};
return this;
};
/**
* Create a redis client from a
* `host:port` uri string.
*
* @param {String} uri
* @return {Client} node client
* @api private
*/
function clientUri(uri){
uri = uri.split(':');
return client(uri[1], uri[0]);
}