blob: 80e64b84d62c905bb92a6ca29932999d5b51ebe3 [file] [log] [blame]
Zack Williamsea89ebb2018-09-07 12:37:36 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17(function () {
18 'use strict';
19
20 const socketIo = require('./websocket.js');
21 const config = require('../config/config.js');
22 const logger = require('../config/logger.js');
23 const socket = socketIo.get();
24
25 // docs: https://github.com/Blizzard/node-rdkafka
26 var kafka = require('node-rdkafka');
27
Matteo Scandolof1523582018-09-28 18:03:09 +020028 const connect = () => {
Zack Williamsea89ebb2018-09-07 12:37:36 -070029
30 try {
Matteo Scandolof1523582018-09-28 18:03:09 +020031 logger.log('debug',`Using librdkafka version: ${kafka.librdkafkaVersion}, kafka features: '${kafka.features}'`);
32 logger.log('debug',`Connecting to broker: ${config.kafka_bootstrap_servers}`);
Zack Williamsea89ebb2018-09-07 12:37:36 -070033
Matteo Scandolof1523582018-09-28 18:03:09 +020034 var stream = kafka.KafkaConsumer.createReadStream({
35 'metadata.broker.list': config.kafka_bootstrap_servers,
36 'group.id': 'xos-ws',
37 'socket.keepalive.enable': true,
38 'enable.auto.commit': false
39 }, {}, {
40 topics: ['xos.gui_events'],
41 });
42
43 stream.on('ready', function () {
44 logger.log('info', 'Kafka connected');
45 });
46
47 stream.on('error', function (err) {
48 logger.log('error', err);
49 });
50
51 stream.consumer.on('event.error', function (err) {
52 logger.log('error', err);
53 });
54
55 stream.on('data', function (msg) {
56 logger.log('debug', `Topic: ${msg.topic}, Key: ${msg.key}, Timestamp: ${msg.timestamp}`);
57
58 // strip diag messages
59 // NOTE: have to coerce to string (due to FFI?)
60 if (msg.key.toString() === 'Diag') {
61 return;
62 }
63 let msgobj;
64
65 try {
66 // TODO find the user that needs to be notified for msg.object update
67 msgobj = JSON.parse(msg.value)
68 }
69 catch(e) {
70 // stringify the event if it is not JSON
71 msgobj = msg.value.toString()
72 }
73
74 if (msgobj.deleted) {
75 logger.log('info', 'Remove on: ' + msg.key + ': ' + msg.value);
76 socket.emit('remove', {model: msg.key.toString(), msg: msgobj, deleted: true});
77 }
78 else {
79 logger.log('info', 'Update on: ' + msg.key + ': ' + msg.value);
80 socket.emit('update', {model: msg.key.toString(), msg: msgobj});
81 }
82
83 });
84 }
Zack Williamsea89ebb2018-09-07 12:37:36 -070085 catch(e) {
Matteo Scandolof1523582018-09-28 18:03:09 +020086 logger.log('warning', 'Failed to connect to kafka, reconnecting in 5 sec', e)
87 setTimeout(connect, 5 * 1000);
Zack Williamsea89ebb2018-09-07 12:37:36 -070088 }
Matteo Scandolof1523582018-09-28 18:03:09 +020089 }
Zack Williamsea89ebb2018-09-07 12:37:36 -070090
Matteo Scandolof1523582018-09-28 18:03:09 +020091 connect()
Zack Williamsea89ebb2018-09-07 12:37:36 -070092
93})();