blob: a051e3697217f63622d88f4797db1fb2547d1ae9 [file] [log] [blame]
Matteo Scandolobd13aab2017-08-08 13:05:24 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080019(function () {
20 'use strict';
21
22 const socketIo = require('./websocket.js');
Matteo Scandolo606751e2017-01-13 13:07:02 -080023 const config = require('../config/config.js');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080024 const logger = require('../config/logger.js');
Matteo Scandolo606751e2017-01-13 13:07:02 -080025 const request = require('superagent');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080026 const socket = socketIo.get();
Matteo Scandolo606751e2017-01-13 13:07:02 -080027 const _ = require('lodash');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080028
29 var redis = require('redis');
Matteo Scandolo292bd622017-01-17 12:07:34 -080030
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080031 var client = redis.createClient({
Matteo Scandolo606751e2017-01-13 13:07:02 -080032 host: config.redis.host,
33 port: config.redis.port
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080034 });
35
36 client.on('error', function (err) {
37 logger.log('error', err);
38 });
39
40 client.on('ready', function () {
41 logger.log('info', 'Redis connected');
42 });
43
44 client.on('subscribe', function (channel) {
45 logger.log('debug', `Subscribed to channel: ${channel}`);
46 });
47
Matteo Scandolo292bd622017-01-17 12:07:34 -080048 client.on('pmessage', function (pattern, channel, message) {
Matteo Scandolof05d8a62016-12-06 13:36:49 -080049 logger.log('warn', 'sub channel ' + channel + ': ' + message);
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080050
51 let msg;
52 try {
53 msg = JSON.parse(message);
54 // TODO find the user that needs to be notified for msg.object update
55 socket.emit('event', {model: channel, msg: msg});
56 }
57 catch(e) {
58 // send the event also if it is not JSON
59 msg = message;
60 socket.emit('event', {model: channel, msg: msg});
61 }
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080062 });
63
Matteo Scandolo292bd622017-01-17 12:07:34 -080064 // subscribe to all channels
65 client.psubscribe('*');
Matteo Scandolo606751e2017-01-13 13:07:02 -080066
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080067})();