Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 input = new PassThrough({ objectMode: true })
  27. const eventType = 'stonk'
  28. const msg = { name: 'BDGRS', price: (500 + Math.floor(Math.random() * 100)).toString(), order: null }
  29. // Write to the input stream
  30. setInterval(() => {
  31. msg.order = Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL'
  32. input.write(msg)
  33. }, 5000)
  34. // h.event() Added at plugin registration
  35. // h is the toolkit
  36. return h.event(input, h, { event: eventType })
  37. },
  38. /** Validate based on validators object */
  39. validate: {
  40. ...validators,
  41. failAction: 'log'
  42. },
  43. },
  44. }