blob: 0bcebe393009cd82db761e267181ab11b32157e5 [file] [log] [blame]
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08001(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 Scandolof05d8a62016-12-06 13:36:49 -080028 logger.log('warn', 'sub channel ' + channel + ': ' + message);
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080029
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})();