Adding a communication channel between server and clients using SSE
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import baseUrl from './utils/baseUrl'
|
||||
import throttle from 'lodash.throttle'
|
||||
|
||||
// TODO https://stackoverflow.com/a/20060461
|
||||
let es = null
|
||||
let dispatchFunc = null
|
||||
|
||||
const getEventStream = () => {
|
||||
if (es === null) {
|
||||
es = new EventSource(
|
||||
baseUrl(`/app/api/events?jwt=${localStorage.getItem('token')}`)
|
||||
)
|
||||
}
|
||||
return es
|
||||
}
|
||||
|
||||
export const startEventStream = (func) => {
|
||||
const es = getEventStream()
|
||||
dispatchFunc = func
|
||||
es.onmessage = throttle(
|
||||
(msg) => {
|
||||
const data = JSON.parse(msg.data)
|
||||
if (data.name !== 'keepAlive') {
|
||||
dispatchFunc(data)
|
||||
}
|
||||
},
|
||||
100,
|
||||
{ trailing: true }
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user