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.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 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 = {
  30. profile_id,
  31. name: 'BDGRS',
  32. price: (500 + Math.floor(Math.random() * 100)).toString(),
  33. order: null,
  34. }
  35. // Write to the input stream
  36. setInterval(() => {
  37. msg.order = Math.floor(Math.random() * 2) === 1 ? 'BUY' : 'SELL'
  38. input.write(msg)
  39. }, 5000)
  40. // h.event() Added at plugin registration
  41. // h is the toolkit
  42. const streamOptions = { event: `${profile_id}.${eventType}` }
  43. return h.event(input, h, streamOptions)
  44. },
  45. /** Validate based on validators object */
  46. validate: {
  47. ...validators,
  48. failAction: 'log',
  49. },
  50. },
  51. }