You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client-supa.ts 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* eslint-disable no-console */
  2. import state from 'state/state'
  3. // import { Shape } from 'types'
  4. import { RealtimeClient, RealtimeSubscription } from '@supabase/realtime-js'
  5. class RoomClient {
  6. id: string
  7. roomId: string
  8. client: RealtimeClient
  9. channel: RealtimeSubscription
  10. lastCursorEventTime = 0
  11. constructor() {
  12. // Create client
  13. this.client = new RealtimeClient(
  14. 'https://mntnflsepfmpvthazvvu.supabase.com'
  15. )
  16. // Set event listeners
  17. this.client.onOpen(() =>
  18. state.send('RT_CHANGED_STATUS', { status: 'Socket opened.' })
  19. )
  20. this.client.onClose(() =>
  21. state.send('RT_CHANGED_STATUS', { status: 'Socket closed.' })
  22. )
  23. this.client.onError((e: Error) =>
  24. state.send('RT_CHANGED_STATUS', { status: `Socket error: ${e.message}` })
  25. )
  26. // Connect to client
  27. this.client.connect()
  28. }
  29. connect(roomId: string) {
  30. this.roomId = roomId
  31. // Unsubscribe from any existing channel
  32. if (this.channel !== undefined) {
  33. this.channel.unsubscribe()
  34. delete this.channel
  35. }
  36. // Create a new channel for this room id
  37. this.channel = this.client.channel(`realtime:public:${this.roomId}`)
  38. this.channel.on('*', (e: any) => console.log(e))
  39. this.channel.on('INSERT', (e: any) => console.log(e))
  40. this.channel.on('UPDATE', (e: any) => console.log(e))
  41. this.channel.on('DELETE', (e: any) => console.log(e))
  42. // Subscribe to the channel
  43. this.channel
  44. .subscribe()
  45. .receive('ok', () => console.log('Connected.'))
  46. .receive('error', () => console.log('Failed.'))
  47. .receive('timeout', () => console.log('Timed out, retrying.'))
  48. // Old
  49. // this.channel = this.pusher.subscribe(
  50. // this.room
  51. // ) as PusherTypes.PresenceChannel
  52. // this.channel.bind('pusher:subscription_error', (err: string) => {
  53. // console.warn(err)
  54. // state.send('RT_CHANGED_STATUS', { status: 'subscription-error' })
  55. // })
  56. // this.channel.bind('pusher:subscription_succeeded', () => {
  57. // const me = this.channel.members.me
  58. // const userId = me.id
  59. // this.id = userId
  60. // state.send('RT_CHANGED_STATUS', { status: 'subscribed' })
  61. // })
  62. // this.channel.bind(
  63. // 'created_shape',
  64. // (payload: { id: string; pageId: string; shape: Shape }) => {
  65. // if (payload.id === this.id) return
  66. // state.send('RT_CREATED_SHAPE', payload)
  67. // }
  68. // )
  69. // this.channel.bind(
  70. // 'deleted_shape',
  71. // (payload: { id: string; pageId: string; shape: Shape }) => {
  72. // if (payload.id === this.id) return
  73. // state.send('RT_DELETED_SHAPE', payload)
  74. // }
  75. // )
  76. // this.channel.bind(
  77. // 'edited_shape',
  78. // (payload: { id: string; pageId: string; change: Partial<Shape> }) => {
  79. // if (payload.id === this.id) return
  80. // state.send('RT_EDITED_SHAPE', payload)
  81. // }
  82. // )
  83. // this.channel.bind(
  84. // 'client-moved-cursor',
  85. // (payload: { id: string; pageId: string; point: number[] }) => {
  86. // if (payload.id === this.id) return
  87. // state.send('RT_MOVED_CURSOR', payload)
  88. // }
  89. // )
  90. }
  91. disconnect() {
  92. this.channel.unsubscribe()
  93. this.client.disconnect()
  94. }
  95. reconnect() {
  96. this.connect(this.roomId)
  97. }
  98. moveCursor(pageId: string, point: number[]) {
  99. if (!this.channel) return
  100. const now = Date.now()
  101. if (now - this.lastCursorEventTime > 200) {
  102. this.lastCursorEventTime = now
  103. this.channel?.trigger('client-moved-cursor', {
  104. id: this.id,
  105. pageId,
  106. point,
  107. })
  108. }
  109. }
  110. }
  111. export default new RoomClient()