Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const socketIo = require('./websocket.js'); |
| 5 | const config = require('../config/config.js').redis; |
| 6 | const logger = require('../config/logger.js'); |
| 7 | const socket = socketIo.get(); |
| 8 | |
| 9 | var redis = require('redis'); |
| 10 | var client = redis.createClient({ |
| 11 | host: config.host, |
| 12 | port: config.port |
| 13 | }); |
| 14 | |
| 15 | client.on('error', function (err) { |
| 16 | logger.log('error', err); |
| 17 | }); |
| 18 | |
| 19 | client.on('ready', function () { |
| 20 | logger.log('info', 'Redis connected'); |
| 21 | }); |
| 22 | |
| 23 | client.on('subscribe', function (channel) { |
| 24 | logger.log('debug', `Subscribed to channel: ${channel}`); |
| 25 | }); |
| 26 | |
| 27 | client.on('message', function (channel, message) { |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 28 | logger.log('warn', 'sub channel ' + channel + ': ' + message); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 29 | |
| 30 | let msg; |
| 31 | try { |
| 32 | msg = JSON.parse(message); |
| 33 | // TODO find the user that needs to be notified for msg.object update |
| 34 | socket.emit('event', {model: channel, msg: msg}); |
| 35 | } |
| 36 | catch(e) { |
| 37 | // send the event also if it is not JSON |
| 38 | msg = message; |
| 39 | socket.emit('event', {model: channel, msg: msg}); |
| 40 | } |
| 41 | |
| 42 | }); |
| 43 | |
| 44 | const watchedCollections = [ |
| 45 | 'Instance', |
| 46 | 'Node', |
| 47 | 'Service', |
| 48 | 'Slice', |
| 49 | 'Site', |
| 50 | 'Subscriber', |
| 51 | 'Tenant' |
| 52 | ]; |
| 53 | |
| 54 | watchedCollections.forEach(c => { |
| 55 | client.subscribe(c); |
| 56 | }); |
| 57 | })(); |