Zack Williams | ea89ebb | 2018-09-07 12:37:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | logger.log('debug',`Using librdkafka version: ${kafka.librdkafkaVersion}, kafka features: '${kafka.features}'`); |
| 29 | logger.log('debug',`Connecting to broker: ${config.kafka_bootstrap_servers}`); |
| 30 | |
| 31 | var stream = kafka.KafkaConsumer.createReadStream({ |
| 32 | 'metadata.broker.list': config.kafka_bootstrap_servers, |
| 33 | 'group.id': 'xos-ws', |
| 34 | 'socket.keepalive.enable': true, |
| 35 | 'enable.auto.commit': false |
| 36 | }, {}, { |
| 37 | topics: ['xos.gui_events'], |
| 38 | }); |
| 39 | |
| 40 | stream.on('ready', function () { |
| 41 | logger.log('info', 'Kafka connected'); |
| 42 | }); |
| 43 | |
| 44 | stream.on('error', function (err) { |
| 45 | logger.log('error', err); |
| 46 | }); |
| 47 | |
| 48 | stream.consumer.on('event.error', function (err) { |
| 49 | logger.log('error', err); |
| 50 | }); |
| 51 | |
| 52 | stream.on('data', function (msg) { |
| 53 | logger.log('debug', `Topic: ${msg.topic}, Key: ${msg.key}, Timestamp: ${msg.timestamp}`); |
| 54 | |
| 55 | // strip diag messages |
| 56 | // NOTE: have to coerce to string (due to FFI?) |
| 57 | if (msg.key.toString() === 'Diag') { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | let msgobj; |
| 62 | |
| 63 | try { |
| 64 | // TODO find the user that needs to be notified for msg.object update |
| 65 | msgobj = JSON.parse(msg.value) |
| 66 | } |
| 67 | |
| 68 | catch(e) { |
| 69 | // stringify the event if it is not JSON |
| 70 | msgobj = msg.value.toString() |
| 71 | } |
| 72 | |
| 73 | if (msgobj.deleted) { |
| 74 | logger.log('info', 'Remove on: ' + msg.key + ': ' + msg.value); |
| 75 | socket.emit('remove', {model: msg.key.toString(), msg: msgobj, deleted: true}); |
| 76 | } |
| 77 | else { |
| 78 | logger.log('info', 'Update on: ' + msg.key + ': ' + msg.value); |
| 79 | socket.emit('update', {model: msg.key.toString(), msg: msgobj}); |
| 80 | } |
| 81 | |
| 82 | }); |
| 83 | |
| 84 | })(); |