sseSource.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { EventSourcePolyfill } from "event-source-polyfill";
  2. class SseSource {
  3. private readonly url: URL
  4. private readonly onOpen: (() => void)
  5. private readonly onMessage: ((eventData: Array<any>) => void)
  6. private readonly onClose: (() => void)
  7. private readonly withCredentials: boolean
  8. private eventSource: EventSource | null = null
  9. constructor(
  10. mercureHubBaseUrl: string,
  11. topic: string,
  12. onOpen: (() => void),
  13. onMessage: ((eventData: Array<any>) => void),
  14. onClose: (() => void),
  15. withCredentials: boolean = true
  16. ) {
  17. this.url = new URL(mercureHubBaseUrl)
  18. this.url.searchParams.append('topic', topic)
  19. this.onOpen = onOpen
  20. this.onMessage = onMessage
  21. this.onClose = onClose
  22. this.withCredentials = withCredentials
  23. }
  24. isConnected () {
  25. return this.eventSource !== null && this.eventSource.readyState === EventSource.OPEN
  26. }
  27. public subscribe () {
  28. if (this.isConnected()) {
  29. throw new Error('SSE - Already subscribed to this event source')
  30. }
  31. if (process.server) {
  32. throw new Error('SSE - Cannot subscribe on server side')
  33. }
  34. this.eventSource = new EventSourcePolyfill(
  35. this.url.toString(),
  36. { withCredentials: this.withCredentials }
  37. );
  38. this.eventSource.onerror = (event) => {
  39. console.error('SSE - An error happened : ' + JSON.stringify(event))
  40. this.unsubscribe()
  41. }
  42. this.eventSource.onopen = () => {
  43. console.log('SSE - Listening for events...')
  44. this.onOpen()
  45. }
  46. this.eventSource.onmessage = event => {
  47. const data = JSON.parse(event.data)
  48. this.onMessage(data)
  49. }
  50. }
  51. public unsubscribe () {
  52. if (this.eventSource === null || this.eventSource.readyState === EventSource.CLOSED) {
  53. return
  54. }
  55. this.eventSource.close()
  56. this.onClose()
  57. console.log('SSE - Subscription closed')
  58. }
  59. }
  60. export default SseSource