Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const socketIo = require('./websocket.js'); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 5 | const config = require('../config/config.js'); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 6 | const logger = require('../config/logger.js'); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 7 | const request = require('superagent'); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 8 | const socket = socketIo.get(); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 9 | const _ = require('lodash'); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 10 | |
| 11 | var redis = require('redis'); |
Matteo Scandolo | 292bd62 | 2017-01-17 12:07:34 -0800 | [diff] [blame] | 12 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 13 | var client = redis.createClient({ |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 14 | host: config.redis.host, |
| 15 | port: config.redis.port |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 16 | }); |
| 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 Scandolo | 292bd62 | 2017-01-17 12:07:34 -0800 | [diff] [blame] | 30 | client.on('pmessage', function (pattern, channel, message) { |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 31 | logger.log('warn', 'sub channel ' + channel + ': ' + message); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 32 | |
| 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 Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 44 | }); |
| 45 | |
Matteo Scandolo | 292bd62 | 2017-01-17 12:07:34 -0800 | [diff] [blame] | 46 | // subscribe to all channels |
| 47 | client.psubscribe('*'); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 48 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 49 | })(); |