A high-performance React application demonstrating real-time order book updates from BitMEX WebSocket API using Signal state management for optimal rendering performance.
🚀 Live Demo - Try it now!
- Real-time Order Book: Live XBTUSD order book data from BitMEX WebSocket API
- Signal State Management: Uses Preact Signals for efficient reactive state updates
- Performance Metrics: Real-time performance monitoring including:
- Messages per second
- Total messages received
- Mid price calculation
- Spread tracking
- Order book depth levels
- Visual Updates: Highlights updated order book levels with animations
- Auto-reconnection: Automatic reconnection with exponential backoff
- Responsive Design: Works on desktop and mobile devices
- React 18: Modern React with TypeScript
- Vite: Fast build tool and dev server
- Preact Signals: Ultra-fast state management
- BitMEX WebSocket API: Real-time market data
- TypeScript: Full type safety
- CSS3: Modern styling with animations
The application uses Preact Signals for state management, which provides:
- Fine-grained reactivity: Only components that actually use changed data re-render
- Computed values: Automatic derivation of sorted order book levels, spread, and mid-price
- Performance optimization: Minimal re-renders even with high-frequency updates
- Memory efficiency: Efficient handling of large order book datasets
- Connects to
wss://ws.bitmex.com/realtime - Subscribes to
orderBookL2:XBTUSDfor level 2 order book data - Handles all WebSocket message types:
partial,insert,update,delete - Implements automatic reconnection with exponential backoff
- Top-of-book focus: Displays top 25 bid/ask levels
- Update highlighting: Visual feedback for changed order levels
- Running totals: Cumulative size calculations
- Real-time metrics: Live performance statistics
- Node.js 16+
- npm or yarn
-
Clone or navigate to the project directory:
cd poc-bitmex-ws-signal-demo -
Install dependencies:
npm install
-
Start the development server:
npm run dev
-
Open your browser to
http://localhost:3000
npm run dev- Start development servernpm run build- Build for productionnpm run preview- Preview production buildnpm run deploy- Deploy to GitHub Pages (manual)
The application automatically deploys to GitHub Pages via GitHub Actions when pushing to the main/master branch.
For manual deployment:
npm run deployThe live demo is available at: https://byshing.github.io/buggy-ws-poc/
src/
├── components/
│ ├── OrderBook.tsx # Order book display component
│ └── PerformanceStats.tsx # Performance metrics display
├── store.ts # Signal-based state management
├── types.ts # TypeScript type definitions
├── App.tsx # Main application component
├── main.tsx # Application entry point
└── index.css # Styling and animations
- Signal-based state management
- WebSocket connection handling
- Order book data processing
- Performance metric tracking
- Computed values for sorted data
- Displays bid and ask sides
- Shows price, size, and cumulative totals
- Highlights recent updates
- Handles large datasets efficiently
- Real-time metrics display
- Updates per second tracking
- Connection status monitoring
- Market data statistics
- Selective Updates: Only components using changed signals re-render
- Computed Efficiency: Derived values update only when dependencies change
- Memory Optimization: Efficient handling of Map-based order book storage
- Batched Updates: Natural batching of rapid WebSocket updates
- Map-based storage for O(1) order book operations
- Limited display levels (top 25) for UI performance
- Debounced update highlighting
- Efficient sorting and totaling with computed signals
- Connection: Establish WebSocket connection to BitMEX
- Subscription: Subscribe to
orderBookL2:XBTUSD - Partial: Receive initial order book snapshot
- Updates: Process incremental updates (
insert,update,delete) - State Management: Update Signal-based state
- UI Updates: Automatic re-rendering of affected components
The application provides real-time performance metrics:
- Messages/sec: WebSocket message throughput
- Total Messages: Cumulative message count
- Mid Price: Calculated mid-market price
- Spread: Bid-ask spread
- Depth: Number of bid/ask levels
- Modern browsers with WebSocket support
- Chrome 60+
- Firefox 55+
- Safari 11+
- Edge 79+
MIT License - Feel free to use this code for learning and demonstration purposes.
This is a demonstration application. BitMEX WebSocket data is used for educational purposes. Always refer to official BitMEX API documentation for production use.