Inside Real-Time Systems
WebSockets, Events, and Collaborative Applications
Dhruv Kothari
Frontend Software Engineer, Postman
DY Patil College of Engineering, Akurdi, Pune·
About me
2022GraduatedDY Patil College of Engineering
2022–24UpraisedSoftware Engineer
2024–25ZomatoSenior Software EngineerPresentPostmanSoftware Engineer
How data travels today
The client asks. The server answers.
The client is the browser or app. The server is the backend. The client makes an API call, and the server sends back the data.
The server only speaks when the client asks.
- 1 · Request
GET /score - 2 · Response
{ "score": "120/3" }
A live cricket score
The score changes on your screen. You did nothing. How?
- Does the page reload?
- Does it call the API every second?
- Or something else?
Four people, one document
Four people edit at the same time. Everyone sees every change.
A normal API call asks once and gets one answer. Here, four people send changes at the same moment.
How would you build this?
Trip to Goa
Day 1: flights land at 11, then the villa.Dhruv
Day 2: beach morning, Fort Aguada at sunset.
Stay: a villa in Anjuna, four rooms.Nishant
Budget: ₹15,000 each, flights included.Harshit
Packing: sunscreen, chargers, ID cards.
Back: train on Sunday night.Kunal
The plan
- 01How data travels.TCP and UDP
- 02How a browser receives updates.Polling, Server-Sent Events and WebSockets
- 03Building a real-time chat.One-to-one, groups, and rooms
- 04Building a collaborative editor.Shared edits and conflicts
Real-time means updates arrive soon enough for the activity.
How data travels
Part 01·TCP and UDP.
TCP
TCP (Transmission Control Protocol) is a fundamental network communication protocol that establishes a reliable, connection-oriented link between two devices to ensure data is delivered accurately and in the correct order.
- Connection setup
- Uses a 3-way handshake (SYN, SYN-ACK, ACK) before it sends any data.
- Speed and overhead
- Larger header (20–60 bytes), plus flow and congestion control. This makes it slower.
- Packet 1
- ACK 1
- Packet 2Lost on the way
- No ACK. Timer runs out
- Packet 2, sent again
- ACK 2
UDP
UDP (User Datagram Protocol) is a lightweight network communication protocol that sends data directly to a destination without establishing a connection, offering maximum speed by not tracking, reordering, or resending lost packets.
- Connection setup
- No handshake. It sends data right away.
- Speed and overhead
- Small, fixed header (8 bytes) and little extra work. This makes it much faster.
- Broadcast and multicast
- Supported. One sender can reach many receivers.
- Datagram 1Received
- Datagram 2Received
- Datagram 3Lost on the wayNever knows
- Datagram 4Received, 3 is missing
How a browser gets updates
Part 02·Polling, Server-Sent Events, and WebSockets.
Short polling
The browser asks for an update at a fixed interval. The server sends the current data, even if nothing changed.
| Polling interval | Effect |
|---|---|
| Shorter | More requests, less waiting |
| Longer | Fewer requests, more waiting |
Hypothetical example
1,000 viewers × 1 req/s= 1,000 requests per second
Use it when updates are rare and a small delay is OK.
- Any new score?
- 120/3 · no change
- Wait 5 s
- Any new score?
- 120/3 · no change
- Wait 5 s
- Any new score?
- 124/3 · new score
Long polling
The browser asks for an update. The server waits until the data changes or a time limit ends. Then the browser asks again.
- Sends fewer replies with no new data.
- Keeps requests open while the server waits.
- Needs a new request after each reply or time limit.
- Needs code to handle lost connections.
Many requests can use the same HTTP connection.
- Any new score?
- Server waits until the score changesRequest stays open
- 124/3 · new score
- Any new score?
- Waits again
Server-Sent Events
Server-Sent Events (SSE) send text updates from the server to the browser over HTTP.
The browser opens the stream. The server sends many updates in one open response.
The browser’s EventSource API connects again by itself. To get missed events, the server must also help.
- Score updates
- Live announcements
- Progress updates
GET /score/stream- 120/3
- 124/3
- 125/3
- Close the stream
Example: AI chat streaming
You use this every day. AI chat apps like Claude and ChatGPT stream their answers.
The browser sends your question once. The server sends the answer back in small pieces, as soon as each piece is ready.
The browser does not ask again. It adds each piece to the screen.
That is why the answer appears word by word.
Explain SSE in one line.
The server keeps one response open and sends each piece as soon as it is ready.
data: "The server" data: " keeps one" data: " response open" data: " and sends" data: " each piece" data: " as soon as" data: " it is ready."
WebSockets
WebSockets is a network communication protocol that provides a full-duplex, bidirectional, long-lived connection between a client and a server over a single TCP socket.
- Starts as a normal HTTP request.
- The server agrees to switch to WebSocket.
- After that, both sides can send at any time.
- 1 · Handshake requestUpgrade: websocket
- 2 · Handshake response101 Switching Protocols
- The TCP connection stays openData frames both ways, no HTTP headers
- 3 · Browser sends a message
- 4 · Server pushes an update
- 5 · Server pushes another
- 6 · Browser sends text
Key characteristics of WebSockets
Bi-directional communication
Either side can send data at any time. It does not wait for a request from the other side.
Persistent connection
The connection stays open until the client or the server closes it.
Ultra-low overhead
After the handshake, there are no bulky HTTP headers, which can be several kilobytes. Data goes in small text or binary frames with only 2 to 10 bytes of overhead. This cuts delay and network traffic.
Its own URL scheme
Uses
ws://(not encrypted) orwss://(encrypted) on the standard ports 80 and 443. This helps it pass through many firewalls.
Comparing the four approaches
| Short polling | Long polling | Server-Sent Events | WebSockets | |
|---|---|---|---|---|
| How it works | Asks again at a fixed interval | Asks, server waits, then asks again | One open response, server keeps sending | One open connection, both sides send |
| Direction | Browser asks, server answers | Browser asks, server answers | Server to browser only | Both ways |
| Connection | New request each time | Request stays open until data or timeout | Stays open | Stays open |
| Delay | Up to one interval | Low | Low | Lowest |
| Overhead | High: many empty replies | Medium: a request per update | Low | Very low: 2 to 10 byte frames |
| Browser API | fetch | fetch | EventSource | WebSocket |
| Good for | Order status, dashboards | Notifications, simple chat | Live scores, AI chat streaming | Chat, games, shared docs |
Building a real-time chat
Part 03·One-to-one chat, group chat, and rooms.
Two people, one server
Dhruv and Sakshi never talk directly.
Each one keeps a WebSocket open to the server. A message goes to the server first. The server relays it to the other person.
- Solid line: sent to the server
- Dotted line: relayed by the server
- Hi
- Hi
- Hello, how are you?
- Hello, how are you?
- I am good!
- I am good!
- Free this weekend?
- Free this weekend?
- Yes, let us plan.
- Yes, let us plan.
A group chat: the Goa trip
Four friends plan a trip. Each message goes to the server. The server sends it to everyone else in the chat.
Goa trip4 members
- NNishantNorth Goa or South?
- HHarshitNorth. Found a villa in Anjuna.
- KKunalTrain tickets are done.
- DDhruvFlights booked for Friday!
"Flights booked for Friday!"DDhruvRooms: many groups, one server
A room is a group of connections. The server keeps the list.
Each message carries a room ID. The server sends it only to the people in that room.
{
"roomId": "goa-trip",
"from": "Dhruv",
"text": "Flights booked!"
}WebSocket has no rooms built in. Our app, or a library like Socket.IO, adds them.
roomId: goa-tripgoa-tripDNHKGets the messageofficeARMNot sentfamilyPSVNot sent
Building a real-time collaborative editor
Part 04·Many people edit the same document, at the same time.
Many cursors, one document
Four people type in the same document at the same time.
Every key press changes the document. Every screen must show the same text, and quickly.
What should happen when Dhruv presses a key?
Trip to Goa
Day 1: flights land at 11, then the villa.Dhruv
Day 2: beach morning, Fort Aguada at sunset.
Stay: a villa in Anjuna, four rooms.Nishant
Budget: ₹15,000 each, flights included.Harshit
Packing: sunscreen, chargers, ID cards.
Back: train on Sunday night.Kunal
One edit, end to end
Send the edit, not the whole document.
- Dhruv types. His browser sends the edit.
- The server applies it and saves it.
- The server sends the edit to Kunal.
- Kunal’s screen updates.
- Solid line: edit sent to the server
- Dotted line: edit sent out by the server
insert "North "- Applies and saves
insert "North "insert " in May"- Applies and saves
insert " in May"- Both screens: “Trip to North Goa in May”
Many people, many edits
Everyone sends edits. The server sends every edit to everyone else.
The server puts the edits in one order. Then every screen applies them in that same order.
- Solid line: edit sent to the server
- Dotted line: edit sent out by the server
Each person gets the edits they did not make.
insert "Day 1"DDhruvdelete line 3KKunalbold "Anjuna"insert "Budget"Two edits, one word
Dhruv and Kunal change the same word at the same moment. Both edits reach the server.
How would you resolve this conflict?
Read more: Operational Transformation (OT) and CRDTs.
OriginalTrip to Goa
- DDhruvSame momentTrip to North Goa
- KKunalSame momentTrip to South Goa
Thank you!
Any questions?
Questions about this talk?
Stuck on a part of it, or curious about how something here works? Ask me anything about it.
Send me a note
Want to grow in your career?
Book a one-on-one session for mentorship and guidance on where to go next.
Book a session on Topmate, opens in a new tab