-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (40 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
51
/*globals module */
var debug = require('debug')('notify:RedisSubscriberAdapter'),
util = require('util'),
EventEmitter = require('events');
(function() {
'use strict';
function RedisSubscriberAdapter(client) {
this.client = client;
this.bindListeners(this.client);
EventEmitter.call(this);
}
RedisSubscriberAdapter.prototype.bindListeners = function(client) {
client.on('error', function(error) {
debug('[ Subscriber: Error ]', { error: error });
this.emit('error', error);
}.bind(this));
client.on('psubscribe', function(key, count) {
debug('[ Subscriber: Subscribed ]', { key: key, count: count });
this.emit('subscribed', key);
}.bind(this));
client.on('punsubscribe', function(key, count) {
debug('[ Subscriber: Unsubscribed ]', { key: key, count: count });
this.emit('unsubscribed', key);
}.bind(this));
client.on('pmessage', function(key, channel, message) {
debug('[ Subscriber: Message ]', { key: key, message: message });
this.emit('message', key, message);
}.bind(this));
};
RedisSubscriberAdapter.prototype.subscribe = function(key) {
debug('[ Subscriber: Subscribing ]', { key: key });
this.client.psubscribe(key);
};
RedisSubscriberAdapter.prototype.unsubscribe = function(key) {
debug('[ Subscriber: Unsubscribing ]', { key: key });
this.client.punsubscribe(key);
};
util.inherits(RedisSubscriberAdapter, EventEmitter);
module.exports = RedisSubscriberAdapter;
}());