Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const Joi = require('joi')
  2. const apiSchema = require('../../schemas/api')
  3. const errorSchema = require('../../schemas/errors')
  4. const Stream = require('stream')
  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: Joi.object({
  15. profile_id: Joi.number(),
  16. })
  17. }
  18. module.exports = {
  19. method: 'GET',
  20. path: '/{profile_id}/subscribe',
  21. options: {
  22. ...pluginConfig.docs,
  23. tags: ['api'],
  24. auth: false,
  25. cors: true,
  26. handler: async (request, h) => {
  27. const input = new PassThrough({ objectMode: true })
  28. const eventType = 'stonk'
  29. const msg = { name: 'BDGRS', price: (500 + Math.floor(Math.random() * 100)).toString(), order: null }
  30. // Write to the input stream
  31. setInterval(() => {
  32. msg.order = Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL'
  33. input.write(msg)
  34. }, 5000)
  35. // h.event() Added at plugin registration
  36. // h is the toolkit
  37. return h.event(input, h, { event: eventType })
  38. },
  39. /** Validate based on validators object */
  40. validate: {
  41. ...validators,
  42. failAction: 'log'
  43. },
  44. },
  45. }