summaryrefslogtreecommitdiff
path: root/frontend/src/utils/websocket.js
blob: 1dde53972eb8ba976233c63fad884b4006004b74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import SockJS from 'sockjs-client'
import Stomp from 'webstomp-client'
import { eventChannel } from 'redux-saga'

const wsURL = '/seven-wonders-websocket'

export const createWsConnection = (headers = {}) => new Promise((resolve, reject) => {
    let socket = Stomp.over(new SockJS(wsURL), {
      debug: process.env.NODE_ENV !== "production"
    })
    socket.connect(headers, (frame) => {
      return resolve({ frame, socket })
    }, reject)
})

export const createSubscriptionChannel = (socket, path) => {
  return eventChannel(emitter => {
    const receiveUsernameHandler = socket.subscribe(path, event => {
      emitter(JSON.parse(event.body))
    })
    return () => receiveUsernameHandler.unsubscribe()
  })
}
bgstack15