You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const Joi = require('joi')
  2. const apiSchema = require('../../schemas/api')
  3. const errorSchema = require('../../schemas/errors')
  4. const params = require('../../schemas/params')
  5. const PassThrough = require('stream').PassThrough
  6. const pluginConfig = {
  7. handlerType: 'notifictaion',
  8. docs: {
  9. description: 'subscribe',
  10. notes: 'Subscribe to notifications based on profile_id',
  11. },
  12. }
  13. const validators = {
  14. params: params.profileId,
  15. }
  16. module.exports = {
  17. method: 'GET',
  18. path: '/{profile_id}/subscribe',
  19. options: {
  20. ...pluginConfig.docs,
  21. tags: ['api'],
  22. auth: false,
  23. cors: true,
  24. handler: async (request, h) => {
  25. const { profile_id } = request.params
  26. const streamName = `${profile_id}.stonk`
  27. const allStreams =
  28. request.server.plugins['notification-plugin']['streams']
  29. allStreams[streamName] = new PassThrough({ objectMode: true })
  30. const msg = {
  31. profile_id,
  32. name: 'BDGRS',
  33. price: (500 + Math.floor(Math.random() * 100)).toString(),
  34. order: null,
  35. type: 'info',
  36. }
  37. // Write to the input stream
  38. msg.order = Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL'
  39. allStreams[streamName].write(msg)
  40. // h.event() Added at plugin registration
  41. // h is the toolkit
  42. return h.event(allStreams[streamName], h, { event: streamName })
  43. },
  44. /** Validate based on validators object */
  45. validate: {
  46. ...validators,
  47. failAction: 'log',
  48. },
  49. },
  50. }