Does Twitter/X Use WebSockets? A Developer's Technical Guide
The technical truth about Twitter's streaming protocol, why it matters for real-time applications, and how WebSocket delivery compares.
The Short Answer: No, Twitter Does Not Use WebSockets
The official X (Twitter) API does not use WebSockets for its streaming endpoints. Instead, it uses HTTP streaming — a long-lived HTTP connection where the server sends data incrementally using chunked transfer encoding. This is a fundamentally different protocol from WebSocket (RFC 6455).
This distinction matters for developers building real-time applications. The protocol you use affects latency, reconnection behavior, bidirectional communication, and how you architect your client code.
How Twitter's HTTP Streaming Actually Works
When you connect to the X API's filtered stream endpoint (GET /2/tweets/search/stream), your client opens a standard HTTP request. The server responds with status 200 and begins writing newline-delimited JSON — one tweet per line, indefinitely. Think of it as downloading an infinitely long file.
Between tweets, X sends keep-alive signals (empty newlines) approximately every 20 seconds. If your client does not receive any data for 20+ seconds, it should assume the connection is stalled and reconnect.
- Protocol: HTTP/1.1 with chunked transfer encoding
- Data format: Newline-delimited JSON (NDJSON)
- Direction: Server to client only (unidirectional)
- Keep-alive: Empty newlines every ~20 seconds
- Authentication: Bearer token in HTTP Authorization header
- Connection: One long-lived HTTP response, never closes (until error or disconnect)
How WebSocket Differs from HTTP Streaming
WebSocket (RFC 6455) is a separate protocol that starts as an HTTP upgrade request and then switches to a framed, bidirectional communication channel. Unlike HTTP streaming, WebSocket supports both server-to-client and client-to-server messages on the same connection.
| Feature | HTTP Streaming (X API) | WebSocket (RFC 6455) |
|---|---|---|
| Initial handshake | Standard HTTP request | HTTP Upgrade → ws:// protocol switch |
| Direction | Server → Client only | Bidirectional |
| Data framing | Newline-delimited chunks | Binary/text frames with length headers |
| Keep-alive | Empty newlines (~20s) | Ping/pong frames (built into protocol) |
| Client can send data | No (new HTTP request required) | Yes (same connection) |
| Disconnect detection | Timeout-based (manual) | Close frames + ping/pong (automatic) |
| Browser support | Requires ReadableStream API | Native WebSocket API |
| Reconnection | Manual implementation required | Libraries handle automatically |
Why This Matters for Real-Time Applications
For trading bots and alert systems, the protocol choice has practical consequences:
- Latency: Both protocols deliver data with similar raw latency. The difference is in overhead — WebSocket frames have 2-14 bytes of overhead vs HTTP chunked encoding's variable overhead.
- Reconnection: HTTP streaming has no built-in mechanism to detect stalled connections. You must implement timeout logic. WebSocket's ping/pong protocol handles this automatically.
- Bidirectional: With HTTP streaming, subscribing to new accounts or changing filters requires a separate HTTP request. With WebSocket, you can send subscription changes on the same connection.
- Browser compatibility: HTTP streaming requires the ReadableStream API for incremental parsing. WebSocket has native browser support and a simpler API.
- Infrastructure: HTTP streaming connections can be interrupted by proxies, load balancers, and CDNs that expect standard request/response patterns. WebSocket connections are designed for long-lived use.
Code Comparison: HTTP Streaming vs WebSocket
WebSocket Services as Twitter Streaming Alternatives
Several third-party services relay Twitter data over true WebSocket connections, bridging the gap between Twitter's HTTP streaming and the WebSocket protocol that most real-time applications prefer.
TweetStream, for example, connects to Twitter data sources server-side and delivers tweet alerts to your application over a standard WebSocket connection. This gives you the familiar WebSocket API (onopen, onmessage, onclose) plus enrichment like OCR, token detection, and live prices.
- Standard WebSocket API — no HTTP streaming parsing required
- Straightforward reconnect handling in client code
- ~200 ms delivery latency including enrichment
- Starts at $199/month
- Bidirectional: subscribe to accounts and change filters on the same connection
Frequently Asked Questions
Sources
TweetStream Team
Last updated: April 2026
Start real-time Twitter WebSocket alerts today
WebSocket delivery, OCR, and token detection - no infrastructure to build.
Start 7-Day TrialFrom $199/mo · Basic/Elite 7-day trial · OCR + token detection included
