Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

chat.service.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import PubNub from 'pubnub'
  2. /**
  3. * Provider method holder
  4. * We always reference this object so
  5. * we don't have to hardcode provider specific
  6. * methods for doing chat things.
  7. *
  8. * This gets overloaded later in the program
  9. */
  10. const providerMethods = {
  11. publish: () => console.error('no provider publish method set'),
  12. subscribe: () => console.error('no provider subscribe method set'),
  13. listen: () => console.error('no provider listen method set')
  14. }
  15. /**
  16. * Breaking out as much pubnub specific flavor
  17. */
  18. const setupPubnub = async uuid => {
  19. if(!uuid) return console.error('no pubnub uuid set')
  20. const pubnubClient = await new PubNub({
  21. publishKey: import.meta.env.VITE_PUBNUB_PUBLISH_KEY,
  22. subscribeKey: import.meta.env.VITE_PUBNUB_SUBSCRIBE_KEY,
  23. uuid
  24. })
  25. // Pass pubnub specific methods to our placeholder obj
  26. providerMethods['publish'] = pubnubClient.publish
  27. providerMethods['subscribe'] = pubnubClient.subscribe
  28. providerMethods['listen'] = pubnubClient.addListener
  29. return pubnubClient
  30. }
  31. class ChatMessage {
  32. constructor({ title, description }) {
  33. this.title = title,
  34. this.description = description
  35. }
  36. }
  37. const testMessage = new ChatMessage({
  38. title: "testing",
  39. description: "hello world!",
  40. })
  41. const MAIN_CHANNEL = 'hello_world'
  42. /** Singleton that holds all our chat information */
  43. class Chatter {
  44. /**
  45. * Create our chatter instance
  46. * @return {Chatter} our chatter instance object
  47. */
  48. constructor() {
  49. // Map of each active chat
  50. this.groupings = {}
  51. // Our pubnub instance
  52. this.provider = null
  53. // UUID used to identify unique users
  54. this.uuid = null
  55. // Setup the main channel
  56. this.subscriptions = [MAIN_CHANNEL]
  57. this.listeners = {
  58. status: async e => {
  59. if (e.category !== "PNConnectedCategory") return
  60. await this.publish(this.subscriptions[0], testMessage)
  61. },
  62. message: this._onMessage,
  63. presence: this._onPresence
  64. }
  65. }
  66. /**
  67. * Callback that fires on every message
  68. * @param {event} e
  69. */
  70. async _onMessage(e) {
  71. console.log(e.message.title)
  72. console.log(e.message.description)
  73. }
  74. async _onPresence(e) {
  75. return
  76. }
  77. async setup(uuid) {
  78. this.uuid = uuid
  79. this.provider = await setupPubnub(this.uuid)
  80. this._listenFor({ listeners: this.listeners })
  81. this._subscribe(this.subscriptions)
  82. }
  83. /**
  84. * Send a message to a channel
  85. * example = new ChatMessage({ title: 'example', description: 'ni' })
  86. * Facade so we can hide provider specific methods
  87. * @param {string} channel
  88. * @param {ChatMessage} message
  89. * @return {object} timestamp
  90. */
  91. async publish(channel, message) {
  92. return await providerMethods['publish']({ channel, message })
  93. }
  94. /**
  95. * Subscribe to a channels
  96. * Facade so we can hide provider specific methods
  97. * @param {array} channels
  98. */
  99. _subscribe(channels) {
  100. providerMethods['subscribe']({ channels })
  101. }
  102. /**
  103. * Listen to events and set callbacks
  104. * Facade so we can hide provider specific methods
  105. */
  106. _listenFor({ listeners }) {
  107. providerMethods['listen'](listeners)
  108. }
  109. }
  110. export { Chatter }