Course contentsShow
← System Design
Lesson 893 of 1,91923. API Gateway PatternFree lesson

WebSocket and Long-Polling Support

Handling persistent connections and real-time communication patterns through the gateway layer.

WebSocket and Long-Polling Support

You'll learn: How API gateways handle persistent connections and real-time communication patterns that differ from traditional request-response cycles.

The Challenge of Real-Time at the Gateway

Traditional HTTP operates on a request-response model: client asks, server responds, connection closes. But modern applications need real-time updates—chat messages, live notifications, streaming data. This introduces persistent connections that stay open, which fundamentally challenges how your API gateway operates.

Think of your gateway like a hotel reception desk. Normally, guests walk up, ask a question, get an answer, and leave. But with WebSockets, some guests want to stand at the desk continuously receiving updates. With long-polling, they keep asking "anything new?" every few seconds. Both patterns require the desk to handle waiting guests differently.

WebSocket Connections Through the Gateway

WebSocket connections start as HTTP but upgrade to a bidirectional protocol. Your gateway must:

  • Recognize upgrade requests and forward them correctly
  • Maintain connection state (breaking the stateless principle)
  • Route messages bidirectionally without breaking the stream
  • Handle connection drops gracefully

The gateway becomes a persistent proxy, not just a request forwarder. This means session affinity—once a WebSocket connects through a gateway instance, it typically stays pinned there.

Long-Polling Considerations

Long-polling keeps HTTP connections open until the server has data to send. Your gateway must:

  • Support extended timeout configurations
  • Avoid connection pooling issues
  • Handle many simultaneous hanging connections efficiently

Gateway Architecture Impact

Supporting these patterns often means the gateway needs specialized connection handling, separate from regular HTTP traffic. Some gateways delegate WebSocket traffic to specific backend services without applying all typical transformations.

Key Takeaway: Real-time communication patterns require your API gateway to handle persistent, stateful connections, which conflicts with stateless gateway design but is essential for modern interactive applications.