Understanding WebSocket: The Foundation of Real-time Web
3 min read
setInterval(function() {
$.get('/v/abc-123/requests')
.then(function(data) {
updateRequestsList(data)
})
}, 5000)
Remember that line of code? Ahh jQuery, the good old days đ
Back in the day, this was how I made my first âreal-timeâ web application. Every 5 seconds, jQuery would ask the server, âHey, do you have any new data?â We called it polling, and honestly, at that time, it felt like magic! The page would update without refreshing - well, kind of.
The problems started when more users began using the app. The server got bombarded with requests every 5 seconds, and users complained that updates were slow. Some even missed important messages because they arrived between polling intervals.
Thatâs when I learned about WebSocket. It completely changed how I build real-time features, and today I want to share what I learned before we add it to WebhookDump. Trust me - itâs way better than our old jQuery polling days!
How WebSocket Makes Things Better
WebSocket fixes this problem in a clever way. Instead of asking for updates over and over, your browser and the server create a connection that stays open, like a phone call. Once connected, they can share information anytime they want.
I like to explain it to my friends like this: Imagine youâre waiting for a package. The old way is like calling the delivery company every few minutes to ask, âIs my package here yet?â WebSocket is like having the delivery person call you the moment your package arrives.
What Actually Happens Behind the Scene?
Let me show you how WebSocket works. It starts with something called a âhandshakeâ - yes, just like when we meet someone new đ

Hereâs whatâs happening:
- First Contact
- Your browser sends a special HTTP request to the server
- Itâs like saying âHey, I know weâre talking HTTP now, but can we switch to WebSocket?â
- This request includes special headers like
Upgrade: websocket
- Server Agreement
- If the server supports WebSocket, it replies âSure, letâs switch!â
- The response has a special code: 101 (Switching Protocols)
- Now both sides upgrade their connection from HTTP to WebSocket
- The Open Line
- After the handshake, the connection stays open
- Both sides can send messages whenever they want
- Messages can be text or binary data
- Each message is called a âframeâ in WebSocket terms
Why This is Cool
The WebSocket connection has some nice features:
- Itâs full-duplex (both sides can talk at the same time)
- It has very little overhead (just a few extra bytes per message)
- It works through firewalls because it starts as a regular HTTP connection
- The connection stays alive as long as both sides want
Real World Example
Letâs look at some basic JavaScript code to use WebSocket:
// Opening a connection
const socket = new WebSocket('ws://example.com/socket');
// Sending a message
socket.send(JSON.stringify({
type: 'chat',
message: 'Hello!'
}));
// Receiving messages
socket.onmessage = function(event) {
const message = JSON.parse(event.data);
console.log('Got message:', message);
};
// Handling connection status
socket.onopen = function() {
console.log('Connected!');
};
socket.onclose = function() {
console.log('Connection closed');
};
No more polling! đ When the server has new data, it just sends it through the open connection. This is perfect for:
- Chat applications
- Live sports scores
- Stock price updates
- Multiplayer games
If youâre curious about WebSocket in action, try opening your browserâs developer tools on a chat app like Slack or Discord. Look at the Network tab - youâll spot these WebSocket connections doing their magic!
On a related note, I recently implemented WebSocket in my project called WebhookDump. Itâs a simple tool that helps developers test and inspect webhook requests in real-time. If youâre interested, head over to webhookdump.link to check it out, or stay tuned for my next posts where I share my journey building it!
Happy coding! đ