S
S
Sergey Suntsev2019-04-29 10:34:30
React
Sergey Suntsev, 2019-04-29 10:34:30

What is the best way to design an eventChannel to receive data from a chat?

I'm using redux-saga, there are channels I accept wrapped in eventChannel

const initSocketChannel = () => eventChannel(emitter => {
  
  socket.on('message', (msg) => {
    const dataEmmit = {value: msg, type: 'message'}
    emitter(dataEmmit)
  })
  
  socket.on('join room', (msg) => {
    const dataEmmit = {value: msg, type: 'join room'}
    emitter(dataEmmit)
  })
  
  socket.on('leave room', (msg) => {
    const dataEmmit = {value: msg, type: 'leave room'}
    emitter(dataEmmit)
  })
  
  socket.on('writing', (msg) => {
    const dataEmmit = {value: msg, type: 'writing'}
    emitter(dataEmmit)
  })
  
  // функция отписки от канала
  return () => {
  
  
  }
})

further I receive in
export const broadcastSaga = function* () {
  const channel = yield call(initSocketChannel)
  while (true) {
    const message = yield take(channel)
    console.log(message)
  }
}

As we can see, both receiving a message and information about users in the room are in one eventChannel The
question is, is it worth making a separate eventChannel for each subscription, or is it better to leave it in one, and already determine which event I'm listening to by type?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question