Handling Outgoing Messages and Actions
Learn how to forward actions in WebSockets.
We'll cover the following...
We must consider how and when to pass actions from Redux to the server:
// A function to hold the WebSocket functionality
return action => {
// TODO: Pass action to server
next(action);
};
Before sending any actions, we need to make sure that the WebSocket is open and ready for transmissions. WebSockets have a readyState
property that returns the current socket status:
// Checking if the socket is open
const SOCKET_STATES = {
CONNECTING: 0,
OPEN: 1,
CLOSING: 2,
CLOSED: 3
};
if (websocket.readyState === SOCKET_STATES.OPEN) {
// Send
}
Even when the socket is open, not all actions have to be sent (for example, actions like TAB_SELECTED
or REST_API_COMPLETE
). ...