forked from ramkumarc/socket.io-mongodb-emitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (128 loc) · 2.72 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
151
152
153
154
155
156
/**
* Module dependencies.
*/
var mubsub = require('mubsub');
var uid = require('uid2')(6);
var parser = require('socket.io-parser');
var hasBin = require('has-binary-data');
var msgpack = require('msgpack-js').encode;
var debug = require('debug')('socket.io-mongodb-emitter');
var mongodbUri = require('mongodb-uri');
/**
* Module exports.
*/
module.exports = Emitter;
/**
* Flags.
*
* @api public
*/
var flags = [
'json',
'volatile',
'broadcast'
];
/**
* Socket.IO mongodb based emitter.
*
* @param {Object} uri (mongo)(optional)
* @param {Object} opts
* @api public
*/
function Emitter(uri, opts){
if (!(this instanceof Emitter)) return new Emitter(uri, opts);
opts = opts || {};
// if passed object
if (typeof uri == 'object') {
opts = uri;
}
var socket = opts.socket;
var client = opts.client;
// init clients if needed
if (!client) client = socket ? mubsub(socket) : mubsub(uri, opts.mongoOpts);
this.client = client;
this.key = (opts.key || 'socket.io');
this.channel = this.client.channel(this.key);
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);
// isolate callback and remove it from the rest of the data
var cb = null;
if(args.length === 3){
cb = args[2];
delete args[2]
}
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.channel.publish(this.key, { uid: uid, data: msgpack([packet, {
rooms: this._rooms,
flags: this._flags
}])}, onPublishComplete);
function onPublishComplete(){
if (typeof(cb) === "function") {
cb();
};
};
// reset state
this._rooms = [];
this._flags = {};
return this;
};
/**
* Close the client connection
*
* @api private
*/
Emitter.prototype.close = function(){
this.client.close();
};