const PassThrough = require('stream').PassThrough const allStreams = {} const activeStream = name => allStreams[name] /** * Takes an open HTTP stream and writes * a msg to it, then fires notification plugin's * onEvent callback * @param {object} msg you want to send * @param {string} name . * @param {object} req server request object * @param {Toolkit} h hapi common response toolkit */ const dispatchNotification = (msg, name, h) => { const stream = activeStream(name) if (!stream) return stream.write(msg) return h.event(stream, h, { event: name }) } const intializeStream = (name, h) => { allStreams[name] = new PassThrough({ objectMode: true }) return dispatchNotification( { profile_id: name, name: 'BDGRS', price: (500 + Math.floor(Math.random() * 100)).toString(), order: Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL', type: 'info', }, name, h, ) } module.exports = { dispatchNotification, allStreams, intializeStream, }