Overview
TheSocketOrderRealtime class provides WebSocket-based real-time communication for order events. It implements the OrderRealtime interface using Socket.IO client and handles automatic reconnection, event subscriptions, and connection lifecycle management.
OrderRealtime Interface
The core interface that defines the contract for real-time order communication:OrderEvents Type
Defines the event handlers for order-related events:Callback invoked when a new order is created. Receives the created order data.
Callback invoked when an order status is updated. Receives the updated order data.
SocketOrderRealtime Class
Location
infraestructure/socket/order-realtime.socket.ts
Constructor
The WebSocket server URL to connect to (e.g.,
http://localhost:3004)Properties
The Socket.IO client instance. Null when disconnected.
Methods
connect()
Establishes a WebSocket connection and subscribes to order events.An object containing event handler callbacks for order events
Connection Configuration
The connection is established with the following settings:Set to
["websocket"] for WebSocket-only transport (no polling fallback)Set to
true to enable automatic reconnection on connection lossMaximum of 5 reconnection attempts before giving up
2000ms (2 seconds) delay between reconnection attempts
Event Subscriptions
The method subscribes to the following Socket.IO events:Emitted when a new order is created. Triggers
events.onCreated() callback with order data.Emitted when an order status changes. Triggers
events.onUpdated() callback with order data.The method removes any existing event listeners before registering new ones to prevent duplicate handlers if
connect() is called multiple times.Implementation
disconnect()
Closes the WebSocket connection and cleans up resources.- Calls
socket.disconnect()to close the connection - Sets the socket instance to
nullto free memory - Should be called when the component unmounts or real-time updates are no longer needed
Usage Example
Here’s how to use the WebSocket client in a React component:Advanced Usage
Reconnection Handling
The client automatically handles reconnection with exponential backoff:Dynamic Event Handling
You can update event handlers by callingconnect() again with new callbacks:
Connection State Monitoring
You can extend the implementation to expose connection state:The socket property is private. For production use, consider adding public methods to expose connection state and health information.
Event Data Format
Bothorder.created and order.status.updated events receive an OrderListDto object:
Implementation Details
Transport Protocol
The client uses WebSocket-only transport (transports: ["websocket"]) to avoid the Socket.IO polling fallback. This provides:
- Lower latency
- Reduced server load
- More efficient real-time updates
Memory Management
The implementation follows best practices for memory management:- Socket instance is reused if
connect()is called multiple times - Old event listeners are removed before adding new ones
- Socket is properly disposed on
disconnect()
Thread Safety
Socket.IO client operations are asynchronous and event-driven. In React applications:- Always clean up connections in
useEffectreturn functions - Consider using refs for socket instances in functional components
- Avoid creating multiple socket instances for the same connection
Related
- HTTP Client - For request/response API communication
- Order Repository - Works alongside WebSocket for data fetching
- Order List DTO - Data structure used in events