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'); |
| 12 | var client = redis.createClient({ |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 13 | host: config.redis.host, |
| 14 | port: config.redis.port |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 15 | }); |
| 16 | |
| 17 | client.on('error', function (err) { |
| 18 | logger.log('error', err); |
| 19 | }); |
| 20 | |
| 21 | client.on('ready', function () { |
| 22 | logger.log('info', 'Redis connected'); |
| 23 | }); |
| 24 | |
| 25 | client.on('subscribe', function (channel) { |
| 26 | logger.log('debug', `Subscribed to channel: ${channel}`); |
| 27 | }); |
| 28 | |
| 29 | client.on('message', function (channel, message) { |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 30 | logger.log('warn', 'sub channel ' + channel + ': ' + message); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 31 | |
| 32 | let msg; |
| 33 | try { |
| 34 | msg = JSON.parse(message); |
| 35 | // TODO find the user that needs to be notified for msg.object update |
| 36 | socket.emit('event', {model: channel, msg: msg}); |
| 37 | } |
| 38 | catch(e) { |
| 39 | // send the event also if it is not JSON |
| 40 | msg = message; |
| 41 | socket.emit('event', {model: channel, msg: msg}); |
| 42 | } |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 43 | }); |
| 44 | |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 45 | // dynamically load Model names to listen on channels |
| 46 | // NOTE how to listen for models defined by services? |
| 47 | request.get(`${config.xos.host}:${config.xos.port}/api/utility/modeldefs`) |
| 48 | .end((err, res) => { |
| 49 | if (err) { |
| 50 | logger.log('error', err); |
| 51 | } |
| 52 | if (res) { |
| 53 | const models = _.map(res.body, i => i.name); |
| 54 | _.forEach(models, c => { |
| 55 | client.subscribe(c); |
| 56 | }); |
| 57 | } |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 58 | }); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 59 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 60 | })(); |