-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
50 lines (43 loc) · 1.5 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
'use strict';
//
// Expose the Metroplex plugin.
//
var Metroplex = module.exports = require('./metroplex');
/**
* Keep the presence or "state" of each connection in Redis.
*
* @param {Primus} primus The Primus instance that received the plugin.
* @param {Object} options The options that were supplied to Primus.
* @api public
*/
Metroplex.server = function server(primus, options) {
var metroplex = new Metroplex(primus, options);
primus.on('connection', function connection(spark) {
metroplex.connect(spark);
}).on('disconnection', function disconnection(spark) {
metroplex.disconnect(spark);
}).on('close', function close(options, next) {
metroplex.unregister(undefined, next);
}).server.on('listening', function listening() {
if (metroplex.address) return;
metroplex.register(primus.server);
});
//
// Register the Metroplex event as `reserved` event so other plugins know
// that they shouldn't be bluntly emitting this and proxy these events to the
// Primus instance you can listen on the Primus server instead of the plugin.
//
['register', 'unregister', 'error'].forEach(function each(event) {
primus.reserved.events[event] = 1;
metroplex.on(event, primus.emits(event));
});
//
// Expose the Metroplex instance so we can interact with it.
//
primus.metroplex = metroplex;
//
// If we have omega-supreme loaded, we're going to introduce additional
// functionality. Full blown, awesome, distributed broadcasting.
//
require('./omega')(primus);
};