Search⌘ K
AI Features

Handling Outgoing Messages and Actions

Explore how to handle outgoing messages and actions in Redux using WebSocket middleware. Learn to check socket states, leverage action metadata to decide what to send, and ensure efficient real-time communication without rebroadcasting issues.

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). ...