Realtime features are everywhere today β chat apps, delivery tracking, payments, notifications.
But one mistake many developers make is:
π Using WebSocket for everything.
In real production systems, we use a mix of:
- Polling
- Server-Sent Events (SSE)
- WebSocket
π The goal is simple: use the simplest solution that works reliably.
π§ 1. What is Realtime Communication?
Realtime means:
π Data updates without refreshing the page
But important point:
π Not all realtime needs to be instant
Some features can tolerate delay (2β5 seconds), and that changes the design.
π 2. Polling (Simple but Powerful)
What is Polling?
Client keeps asking server for updates:
setInterval(async () => {
const res = await fetch("/status");
const data = await res.json();
console.log(data);
}, 5000);
When Polling Works Best
Use polling when:
- Data changes slowly
- You need high reliability
- Simplicity is preferred
Real Example: Payment Status
Letβs understand this properly.
Flow:
- User clicks βPayβ
- Redirected to third-party gateway (Razorpay / Stripe)
- Payment happens outside your app
- Gateway sends webhook β your backend
- Backend updates DB
- Frontend checks status
Why Polling is Used Here
- Frontend doesnβt control payment result
- Gateway response timing is unpredictable
- User may refresh or leave page
π So frontend polls backend:
setInterval(async () => {
const res = await fetch("/payment/status?id=123");
const data = await res.json();
if (data.status === "success") {
// stop polling
}
}, 3000);
Why Not WebSocket or SSE?
- Requires active connection
- Not reliable across page refresh
- Overkill for single status change
π Polling wins because it is simple and reliable
π 3. WebSocket (For True Realtime)
What is WebSocket?
- Persistent connection
- Two-way communication
π Both client and server can send data anytime
When to Use WebSocket
- Chat apps
- Live tracking (driver, delivery)
- Multiplayer apps
Reconnection Problem (Real Issue)
Example:
- User offline for 1 hour
- 1000 updates missed
Problems:
- Missing data
- Duplicate data
- Wrong order
Real-world Solution Pattern
- Use message IDs
- Store last received ID
- Fetch missing data after reconnect
fetch(`/messages?afterId=${lastId}`);
WhatsApp Example (Important)
- Uses message IDs + ACK
- Stores messages locally
- Syncs missing messages after reconnect
- Loads messages in chunks (avoid UI freeze)
π Large data is never pushed blindly
π‘ 4. Server-Sent Events (SSE)
What is SSE?
- Server pushes data to client
- One-way communication
const eventSource = new EventSource("/events");
eventSource.onmessage = (event) => {
console.log(event.data);
};
When to Use SSE
- Notifications
- Live dashboards
- Activity feeds
- Jenkins pipeline updates
- Live cricket/sports score/commentary updates
Limitations
- Client cannot send data
- Not suitable for interactive systems
βοΈ 5. Polling vs SSE vs WebSocket
| Feature | Polling | SSE | WebSocket |
|---|---|---|---|
| Direction | Client β Server | Server β Client | Two-way |
| Complexity | Low | Medium | High |
| Realtime | Low | Medium | High |
| Use case | Status | Notifications | Chat/Tracking |
π§ 6. Real-world Scenarios (How to Choose)
This is where real engineering decisions happen.
π Driver Tracking (Uber / Ola)
Flow:
- Driver app sends location β Backend
- Backend calculates ETA
- Backend sends updates β User app
Why WebSocket is Commonly Used
Driver side needs to:
- Send frequent location updates
- Receive updates (route change, cancel, reassign)
π This becomes two-way communication β WebSocket fits well
Can We Avoid WebSocket?
Yes, there are alternatives.
Option 1: Driver β REST API (Polling/Push)
- Driver sends location every few seconds via HTTP
- Backend processes and stores data
Option 2: Backend β User via SSE
- User receives updates via SSE
π This works if:
- No real-time interaction required
- Slight delay is acceptable
Trade-off
| Approach | Pros | Cons |
|---|---|---|
| WebSocket | Low latency, real-time | More complex |
| REST + SSE | Simpler infra | Slight delay, more requests |
π Real apps choose based on scale and need
π Zomato / Swiggy
π‘ Order Status (Placed β Delivered)
- Low frequency updates
- Delay is acceptable
π Polling is usually used
Can SSE Work?
Yes, and some systems may use it.
Flow:
- Backend updates DB
- SSE pushes new status
- Client updates UI
π But:
- Needs persistent connection
- More infra setup
π Polling is simpler and reliable
π’ Delivery Tracking (Map Movement)
Flow:
- Delivery partner β Backend (location updates)
- Backend β User (location + ETA)
Common Assumption
π βUser only needs data, so SSE is enoughβ
This is partially correct.
Real-world Consideration
Even user app may need to:
- Cancel order
- Change address
- Trigger actions
π These go via REST APIs
Possible Architectures
Option 1 (Simpler)
- Driver β REST (send location)
- Backend β SSE (push updates to user)
- User β REST (actions like cancel)
π Works well for moderate scale
Option 2 (Advanced / High Scale)
- Driver β Backend β WebSocket
- User β Backend β WebSocket
π Benefits:
- Lower latency
- Unified system
- Easier event handling
Why Many Apps Still Use WebSocket
- One consistent system
- Handles all events (location, cancel, updates)
- Better performance at scale
π Even if SSE + REST is possible, WebSocket simplifies architecture
ποΈ 7. Stateless vs Stateful Architecture (Very Important)
Understanding this helps you choose the right protocol.
Stateless (REST APIs)
- Each request is independent
- Server does not store connection state
Example:
GET /order/status
π Server responds and forgets
Pros
- Easy to scale
- Works with load balancers
- Reliable
Cons
- Not efficient for realtime
- Requires repeated requests (polling)
Stateful (WebSocket)
- Connection stays open
- Server maintains session/connection state
π Server knows client is connected
Pros
- Real-time communication
- Low latency
Cons
- Harder to scale
- Needs connection management
SSE (Hybrid)
- Uses HTTP (stateless)
- But keeps connection open
π Semi-stateful
π 8. REST vs Realtime APIs
REST APIs
- Request β Response
- Stateless
- Works well for CRUD operations
Realtime APIs (WebSocket / SSE)
- Event-driven
- Continuous data flow
When to Use What
- REST β actions (create, update, cancel)
- WebSocket/SSE β live updates
π Most apps use both together
π§ Decision Framework
Ask these questions:
Do I need two-way communication?
π Yes β WebSocket
π No β SSE or PollingHow frequent are updates?
π Low β Polling
π Medium β SSE
π High β WebSocketDo I need simplicity or performance?
π Simplicity β Polling
π Performance β WebSocket
π§Ύ Conclusion
In real systems:
- Polling β simple and reliable
- SSE β simple realtime push
- WebSocket β complex realtime interaction
π Most systems combine:
- REST for actions
- Realtime for updates
Final Thought
There is no single best solution.
Good engineering is about choosing the right tool for the job.
π Start simple
π Scale when needed





Top comments (0)