-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.js
37 lines (28 loc) · 882 Bytes
/
pubsub.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
const redis = require('redis');
class PubSub {
constructor() {
this.publisher = redis.createClient(xxxx,'xx.xx.xx.xx');
this.subscriber = redis.createClient(xxxx,'xx.xx.xx.xx');
this.publisher.on('error', (error) => this.handleError(error));
this.subscriber.on('message', (channel, message) => this.handleMessage(channel, message));
this.subscriber.on('error', (error) => this.handleError(error));
}
handleMessage(channel, message) {
console.log(`Message received: ${channel} ${message}`);
}
handleError(error) {
console.log(`Error ${error}`);
}
subscribeToChannels(channels) {
Object.values(channels).forEach((channel) => {
this.subscriber.subscribe(channel);
});
}
publish({ channel, message }) {
this.publisher.publish(channel, message);
}
subscribe(channel) {
this.subscriber.subscribe(channel);
}
}
module.exports = PubSub;