| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- const Joi = require('joi')
- const apiSchema = require('../../schemas/api')
- const errorSchema = require('../../schemas/errors')
- const params = require('../../schemas/params')
-
- const Stream = require('stream')
- 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 input = new PassThrough({ objectMode: true })
- const eventType = 'stonk'
-
- const msg = {
- profile_id,
- name: 'BDGRS',
- price: (500 + Math.floor(Math.random() * 100)).toString(),
- order: null,
- type: 'info',
- }
-
- // Write to the input stream
- setInterval(() => {
- msg.order = Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL'
- input.write(msg)
- }, 5000)
-
- // h.event() Added at plugin registration
- // h is the toolkit
- const streamOptions = { event: `${profile_id}.${eventType}` }
- return h.event(input, h, streamOptions)
- },
-
- /** Validate based on validators object */
- validate: {
- ...validators,
- failAction: 'log',
- },
- },
- }
|