-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
236 lines (206 loc) · 6.64 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Kafka adapter for socket.io
*
* @example <caption>Example usage</caption>
* var io = require('socket.io')(3000);
* var kafka = require('socket.io-kafka');
* io.adapter(kafka('localhost:2181'));
*
* @module socket.io-kafka
* @see {@link https://www.npmjs.com/package/kafka-node|kafka-node}
*
* @author Guilherme Hermeto
* @licence {@link http://opensource.org/licenses/MIT|MIT}
*/
/*jslint node: false */
'use strict';
var kafka = require('kafka-node'),
Adapter = require('socket.io-adapter'),
debug = require('debug')('socket.io-kafka'),
async = require('async'),
uid2 = require('uid2');
/**
* Generator for the kafka Adapater
*
* @param {string} optional, zookeeper connection string
* @param {object} adapter options
* @return {Kafka} adapter
* @api public
*/
function adapter(uri, options) {
var opts = options || {},
prefix = opts.key || 'socket.io',
uid = uid2(6),
client;
// handle options only
if ('object' === typeof uri) {
opts = uri;
uri = opts.uri || opts.host ? opts.host + ':' + opts.port : null;
if (!uri) { throw new URIError('URI or host/port are required.'); }
}
// create producer and consumer if they weren't provided
if (!opts.producer || !opts.consumer) {
debug('creating new kafa client');
client = new kafka.Client(uri, opts.clientId, { retries: 2 });
if (!opts.producer) {
debug('creating new kafa producer');
opts.producer = new kafka.Producer(client);
}
if (!opts.consumer) {
debug('creating new kafa consumer');
opts.consumer = new kafka.Consumer(client, [], { groupId: prefix });
}
}
/**
* Kafka Adapter constructor.
*
* @constructor
* @param {object} channel namespace
* @api public
*/
function Kafka(nsp) {
var self = this,
create = opts.createTopics;
Adapter.call(this, nsp);
this.uid = uid;
this.options = opts;
this.prefix = prefix;
this.consumer = opts.consumer;
this.producer = opts.producer;
this.mainTopic = prefix + nsp.name;
opts.createTopics = (create === undefined) ? true : create;
opts.producer.on('ready', function () {
debug('producer ready');
self.createTopic(self.mainTopic);
self.subscribe(self.mainTopic);
// handle incoming messages to the channel
self.consumer.on('message', self.onMessage.bind(self));
self.consumer.on('error', self.onError.bind(self));
});
}
// inherit from Adapter
Kafka.prototype = Object.create(Adapter.prototype);
Kafka.prototype.constructor = Kafka;
/**
* Emits the error.
*
* @param {object|string} error
* @api private
*/
Kafka.prototype.onError = function (err) {
var self = this,
arr = [].concat.apply([], arguments);
if (err) {
debug('emitting error', err);
arr.forEach(function (error) { self.emit('error', error); });
}
};
/**
* Process a message received by a consumer. Ignores messages which come
* from the same process.
*
* @param {object} kafka message
* @api private
*/
Kafka.prototype.onMessage = function (kafkaMessage) {
var message, packet;
try {
message = JSON.parse(kafkaMessage.value);
if (uid === message[0]) { return debug('ignore same uid'); }
packet = message[1];
if (packet && packet.nsp === undefined) {
packet.nsp = '/';
}
if (!packet || packet.nsp !== this.nsp.name) {
return debug('ignore different namespace');
}
this.broadcast(packet, message[2], true);
} catch (err) {
// failed to parse JSON?
this.onError(err);
}
};
/**
* Converts a socket.io channel into a safe kafka topic name.
*
* @param {string} cahnnel name
* @return {string} topic name
* @api private
*/
Kafka.prototype.safeTopicName = function (channel) {
return channel.replace('/', '_');
};
/**
* Uses the producer to create a new tpoic synchronously if
* options.createTopics is true.
*
* @param {string} topic to create
* @api private
*/
Kafka.prototype.createTopic = function (channel) {
var chn = this.safeTopicName(channel);
debug('creating topic %s', chn);
if (this.options.createTopics) {
this.producer.createTopics(chn, this.onError.bind(this));
}
};
/**
* Uses the consumer to subscribe to a topic.
*
* @param {string} topic to subscribe to
* @param {Kafka~subscribeCallback}
* @api private
*/
Kafka.prototype.subscribe = function (channel, callback) {
var self = this,
p = this.options.partition || 0,
chn = this.safeTopicName(channel);
debug('subscribing to %s', chn);
self.consumer.addTopics([{topic: chn, partition: p}],
function (err) {
self.onError(err);
if (callback) { callback(err); }
});
};
/**
* Uses the producer to send a message to kafka. Uses snappy compression.
*
* @param {string} topic to publish on
* @param {object} packet to emit
* @param {object} options
* @api private
*/
Kafka.prototype.publish = function (channel, packet, opts) {
var self = this,
msg = JSON.stringify([self.uid, packet, opts]),
chn = this.safeTopicName(channel);
this.producer.send([{ topic: chn, messages: [msg], attributes: 2 }],
function (err, data) {
debug('new offset in partition:', data);
self.onError(err);
});
};
/**
* Broadcasts a packet.
*
* If remote is true, it will broadcast the packet. Else, it will also
* produces a new message in one of the kafka topics (channel or rooms).
*
* @param {object} packet to emit
* @param {object} options
* @param {Boolean} whether the packet came from another node
* @api public
*/
Kafka.prototype.broadcast = function (packet, opts, remote) {
var self = this,
channel;
debug('broadcasting packet', packet, opts);
Adapter.prototype.broadcast.call(this, packet, opts);
if (!remote) {
channel = self.safeTopicName(self.mainTopic);
self.publish(channel, packet, opts);
}
};
return Kafka;
}
module.exports = adapter;