Home > other >  How to get websocket data as state in redux?
How to get websocket data as state in redux?

Time:07-25

My task is to set a websocket and get its data with onMessage() and assign it to redux state. But I don`t know how to implement it. What is the solution?

CodePudding user response:

You have to dispatch you actions inside your event listener:

// Listen for messages
socket.addEventListener('message', (socketData) => {
  //event is data passed from socket to you

  //dispatch your data accordingly
  store.dispatch({
    type: 'Read the docs',
    payload: socketData
  })
});

  • Related