const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const params = require('../../schemas/params') const PassThrough = require('stream').PassThrough const pluginConfig = { handlerType: 'notifictaion', docs: { description: 'subscribe', notes: 'Subscribe to notifications based on profile_id', }, } const validators = { params: params.profileId, } module.exports = { method: 'GET', path: '/{profile_id}/subscribe', options: { ...pluginConfig.docs, tags: ['api'], auth: false, cors: true, handler: async (request, h) => { const { profile_id } = request.params const streamName = `${profile_id}.stonk` const allStreams = request.server.plugins['notification-plugin']['streams'] allStreams[streamName] = new PassThrough({ objectMode: true }) const msg = { profile_id, name: 'BDGRS', price: (500 + Math.floor(Math.random() * 100)).toString(), order: null, type: 'info', } // Write to the input stream msg.order = Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL' allStreams[streamName].write(msg) // h.event() Added at plugin registration // h is the toolkit return h.event(allStreams[streamName], h, { event: streamName }) }, /** Validate based on validators object */ validate: { ...validators, failAction: 'log', }, }, }