Interactive Brokers gives you two API options for Node.js - socket-based TWS and REST-based Client Portal. Pick based on what you need: real-time data (use TWS) or something that deploys nicely (use REST). Your choice affects everything else.
TWS API Architecture Overview
TWS API: Socket-Based Real-Time Integration
The TWS (Trader Workstation) API is your gateway to IB's full feature set, assuming you can keep the connection alive long enough to use it. Built on TCP socket connections, it needs either the TWS desktop app or IB Gateway running somewhere - and if you think "somewhere" means a cloud server, prepare for pain.
The TWS API works when it wants to, but when it's up:
- Sub-millisecond latency on market data - genuinely fast
- Full TWS feature set through the API
- Standard Node.js events, no weird callback patterns
- One login session lasts all day
The **@stoqey/ib** library is your best bet - it ports IB's Java Client Version 10.32.01 to Node.js with actual TypeScript support. Released October 2024, it has proper async/await instead of callback hell. The GitHub repo has working examples and decent docs with TypeScript definitions.
Socket Connection Ports:
- IB Gateway Live:
4001
- IB Gateway Paper Trading:
4002
- TWS Live Account:
7496
- TWS Paper Trading:
7497
Client Portal Web API: REST-Based Cloud Integration
IB Gateway Configuration
The Client Portal Web API is REST-based, so no desktop software bullshit. Works great in cloud deployments where you can't run GUI apps.
REST API Advantages:
- Stateless design that actually works with serverless and containers
- OAuth 2.0 authentication that doesn't make you want to throw your laptop
- Standard HTTP/HTTPS protocols your firewall won't hate
- JSON responses instead of whatever the fuck TWS sends over sockets
REST API downsides: slower than it should be and missing some TWS features. The Client Portal Gateway provides the backend infrastructure, while the OAuth implementation guide covers secure authentication patterns.
Hybrid Integration Patterns
Most production setups use both APIs:
- TWS API for real-time market data and low-latency order execution
- Client Portal API for account management and portfolio reporting
- Message queues (Redis, RabbitMQ) bridging the architectural patterns
This way you get everything TWS offers without losing your mind deploying it. When TWS inevitably shits the bed (and it will), you can fail over to REST and pretend everything's fine.
Performance and Scalability Considerations
TWS is single-threaded because apparently it's 1999, so you're stuck with one connection per client ID. Want to scale? Good luck juggling multiple client IDs and connection pools. The @stoqey/ibkr wrapper tries to make this less painful with automatic reconnection, but you're still dealing with IB's ancient architecture decisions.
Resource Requirements:
- TWS GUI: ~200MB memory usage
- IB Gateway: ~120MB memory (40% less than TWS)
- Node.js client: ~50MB additional per connection
Production deployments typically use IB Gateway in containerized environments, eliminating GUI overhead while maintaining full API functionality.
Docker IB Gateway Deployment
The @stoqey/ib library documentation leaves a lot to be desired. Connections drop without warning - spent 8 hours once debugging why trades weren't going through, turns out IB changed their heartbeat timeout and didn't tell anyone. Orders fail with error code -1 and zero context. Gateway uses less memory than TWS but TWS gives you better debug info when shit breaks.
Production Reality Check: Data subscriptions expire without warning during live trading. Had my algorithm running on stale prices for 20 minutes before I caught it - P&L was completely fucked. Lost maybe 3 grand because error code 354 means "your market data expired" but the docs just say "invalid request." Their error code reference is useless.