-
Notifications
You must be signed in to change notification settings - Fork 48
/
redisSubscriber.js
40 lines (36 loc) · 1.19 KB
/
redisSubscriber.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
/**
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
/**
* ./realTime/redisSubscriber.js
*/
'use strict'; // eslint-disable-line strict
const emitter = require('./socketIOEmitter');
const sub = require('../cache/redisCache').client.sub;
const featureToggles = require('feature-toggles');
/**
* Redis subscriber uses socket.io to broadcast.
*
* @param {Socket.io} io - Socket.io's Server API
* @param {Object} sub - Redis subscriber instance
*/
module.exports = (io) => {
sub.on('message', (channel, mssgStr) => {
if (featureToggles.isFeatureEnabled('instrumentRealtimeEvents')) {
console.log(`[RT] subscribeTimestamp=${(new Date()).toISOString()} ` +
`size=${mssgStr.length}`);
}
// message object to be sent to the clients
const mssgObj = JSON.parse(mssgStr);
const key = Object.keys(mssgObj)[0];
/*
* pass on the message received through the redis subscriber to the socket
* io emitter to send data to the browser clients.
*/
emitter(io, key, mssgObj);
});
};