blob: 5f636327708554b06f7ef9b24643f534c9d5018c [file] [log] [blame]
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08001(function () {
2 'use strict';
3
4 const socketIo = require('./websocket.js');
Matteo Scandolo606751e2017-01-13 13:07:02 -08005 const config = require('../config/config.js');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08006 const logger = require('../config/logger.js');
Matteo Scandolo606751e2017-01-13 13:07:02 -08007 const request = require('superagent');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08008 const socket = socketIo.get();
Matteo Scandolo606751e2017-01-13 13:07:02 -08009 const _ = require('lodash');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080010
11 var redis = require('redis');
Matteo Scandolo292bd622017-01-17 12:07:34 -080012
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080013 var client = redis.createClient({
Matteo Scandolo606751e2017-01-13 13:07:02 -080014 host: config.redis.host,
15 port: config.redis.port
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080016 });
17
18 client.on('error', function (err) {
19 logger.log('error', err);
20 });
21
22 client.on('ready', function () {
23 logger.log('info', 'Redis connected');
24 });
25
26 client.on('subscribe', function (channel) {
27 logger.log('debug', `Subscribed to channel: ${channel}`);
28 });
29
Matteo Scandolo292bd622017-01-17 12:07:34 -080030 client.on('pmessage', function (pattern, channel, message) {
Matteo Scandolof05d8a62016-12-06 13:36:49 -080031 logger.log('warn', 'sub channel ' + channel + ': ' + message);
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080032
33 let msg;
34 try {
35 msg = JSON.parse(message);
36 // TODO find the user that needs to be notified for msg.object update
37 socket.emit('event', {model: channel, msg: msg});
38 }
39 catch(e) {
40 // send the event also if it is not JSON
41 msg = message;
42 socket.emit('event', {model: channel, msg: msg});
43 }
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080044 });
45
Matteo Scandolo292bd622017-01-17 12:07:34 -080046 // subscribe to all channels
47 client.psubscribe('*');
Matteo Scandolo606751e2017-01-13 13:07:02 -080048
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080049})();