If You Are Used to Polling APIs
Previously, you might have sent a GET request every few minutes to receive a list of recent events. With WebSockets, you:
- Connect once.
- Authenticate by sending a
connection_initJSON message with your oAuth token. - Send a subscription request.
- Send regular
pingrequests and reply withpongto serverpingsto keep the connection open. - Wait and process events as they stream in. There is no need to
poll. Each event will be a JSON
nextmessage. - Exchange heartbeat messages to keep the connection open.
- When done, send a
completemessage, but wait for the server to close the connection.
You only need to reconnect if the connection drops or if your authentication expires.
| Polling API | Streaming API (WebSocket) | |
|---|---|---|
| API Complexity | Simple | Requires ping/pong, reconnection, etc. |
| Connection | New connection for each poll | Persistent once-open connection |
| Data Arrival | On schedule (e.g., every N minutes) | Real-time (as soon as event happens) |
| Network Load | High (many HTTP requests) | Low (connection stays open) |
| Sample Output | List of events; may contain duplicates | Single event per message; processed sequentially |
Parent topic: Overview of Streaming API