Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 Stream = require('stream')
  6. const PassThrough = require('stream').PassThrough
  7. const pluginConfig = {
  8. handlerType: 'notifictaion',
  9. docs: {
  10. description: 'subscribe',
  11. notes: 'Subscribe to notifications based on profile_id',
  12. },
  13. }
  14. const validators = {
  15. params: params.profileId
  16. }
  17. module.exports = {
  18. method: 'GET',
  19. path: '/{profile_id}/subscribe',
  20. options: {
  21. ...pluginConfig.docs,
  22. tags: ['api'],
  23. auth: false,
  24. cors: true,
  25. handler: async (request, h) => {
  26. const { profile_id } = request.params
  27. const input = new PassThrough({ objectMode: true })
  28. const eventType = 'stonk'
  29. const msg = { profile_id, 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. const streamOptions = { event: `${profile_id}.${eventType}` }
  38. return h.event(input, h, streamOptions)
  39. },
  40. /** Validate based on validators object */
  41. validate: {
  42. ...validators,
  43. failAction: 'log'
  44. },
  45. },
  46. }