-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
197 lines (179 loc) · 5.08 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Module dependencies.
*/
var uid2 = require('uid2');
var r = require('rethinkdb');
require('rethinkdb-init')(r);
var Adapter = require('socket.io-adapter');
var Emitter = require('events').EventEmitter;
var debug = require('debug')('socket.io-rethinkdb');
/**
* Module exports.
*/
module.exports = adapter;
/**
* Returns a RethinkDB Adapter class.
*
* @param {String} optional, rethinkdb uri
* @return {RethinkDBAdapter} adapter
* @api public
*/
function adapter(uri, opts){
opts = opts || {};
// handle options only
if ('object' == typeof uri) {
opts = uri;
uri = null;
}
// handle uri string
if (uri) {
uri = uri.split(':');
opts.host = uri[0];
opts.port = uri[1];
}
// opts
var socket = opts.socket;
var host = opts.host || '127.0.0.1';
var port = +(opts.port || 28015);
var db = opts.db || 'socketio_rethinkdb';
var conn_opts = { host: host, port: port, db: db };
var save_messages = +(opts.save_messages || false);
var durability_hard = +(opts.durability || false);
var durability = (durability_hard) ? 'hard' : 'soft';
// this server's key
var server_uid = uid2(6);
var errorHandler = function (err) {
debug(err);
throw err;
};
/**
* Adapter constructor.
*
* @param {String} namespace name
* @api public
*/
function RethinkDBAdapter(nsp){
Adapter.call(this, nsp);
var self = this;
this.init = r.init(conn_opts, [ 'messages' ])
.then(function (conn) {
return Promise.resolve()
.then(function () {
return r.table('messages')
// Don't listen to messages from this server
.filter(r.row('server_uid').ne(server_uid))
.changes({ squash: false })
.run(conn)
.then(function (cursor) {
cursor.each(function (err, change) {
// Only listen to inserts
if (change.old_val === null) {
if (err) {
debug(err);
self.emit('error', err);
}
//var opts = JSON.parse(change.new_val.opts);
this.onmessage(null, change.new_val.message, change.new_val.opts);
}
}.bind(this));
}.bind(this));
}.bind(this));
}.bind(this))
.catch(errorHandler);
}
/**
* Inherits from `Adapter`.
*/
RethinkDBAdapter.prototype.__proto__ = Adapter.prototype;
/**
* Called with a subscription message
*
* @api private
*/
RethinkDBAdapter.prototype.onmessage = function(pattern, msg, opts){
if (msg && msg.nsp === undefined && msg.nsp !== null) {
msg.nsp = '/';
}
if (!msg || msg.nsp != this.nsp.name) {
return debug('ignore different namespace');
}
this.broadcast.apply(this, [msg, opts, true]);
};
/**
* Convert all undefined values to null
*
* @param {Object}
* @return {Object}
*/
RethinkDBAdapter.prototype.remove_undefined = function (obj) {
if (Array.isArray(obj)) {
obj.forEach(function (val, i) {
if (val === undefined) {
obj.splice(i, 1);
}
});
return obj.map(function (value) {
return this.remove_undefined(value);
}.bind(this));
} else if (typeof obj === 'object' && obj !== null) {
for (var key in obj) {
if (obj[key] === undefined) {
delete obj[key];
} else {
obj[key] = this.remove_undefined(obj[key]);
}
}
return obj;
} else {
return obj;
}
};
/**
* Broadcasts a packet.
*
* @param {Object} packet to emit
* @param {Object} options
* @param {Boolean} whether the packet came from another node
* @api public
*/
RethinkDBAdapter.prototype.broadcast = function(packet, opts, remote){
var self = this;
Adapter.prototype.broadcast.call(this, packet, opts);
if (!remote) {
if (opts.rooms === undefined) opts.rooms = null;
return this.init.then(function () {
return r.connect(conn_opts).then(function (conn) {
/**
* Important:
*
* RethinkDB doesn't save values that are defined as `undefined` (which
* makes all the sense in the world, if you ask me). Hence, in order
* to make sure our data will get saved correctly, we delete all
* values equal to `undefined`.
*/
packet = self.remove_undefined(packet);
opts = self.remove_undefined(opts);
return r.db(conn_opts.db).table('messages').insert({
server_uid: server_uid,
message: packet,
opts: opts
})
.run(conn, { durability: durability })
.then(function (res) {
// Delete all keys
if (save_messages) return true;
return r.db(conn_opts.db).table('messages')
.getAll(r.args(res.generated_keys))
.delete()
.run(conn, { durability: durability });
})
.then(function () {
conn.close();
});
});
})
.catch(errorHandler);
}
};
return RethinkDBAdapter;
}