What if your app could magically get updates the moment they happen, without asking again and again?
Why Long polling and Server-Sent Events in HLD? - Purpose & Use Cases
Imagine you have a website that shows live sports scores. Without any special technique, the website must keep asking the server every few seconds, "Any new score?" This is like repeatedly knocking on a door to check if someone is home.
This constant asking wastes time and energy. The server gets overwhelmed with many repeated requests, and users may see delays or outdated scores. It feels slow and clunky, like waiting in line to ask the same question over and over.
Long polling and Server-Sent Events let the server tell the user immediately when there is new information. Instead of asking repeatedly, the user waits patiently, and the server sends updates as soon as they happen. This saves effort and makes updates feel instant.
while(true) { fetch('/score'); await new Promise(resolve => setTimeout(resolve, 5000)); }
const eventSource = new EventSource('/scores');
eventSource.onmessage = e => updateScore(e.data);This approach enables real-time updates that feel smooth and instant, improving user experience and reducing server load.
News websites use Server-Sent Events to push breaking news headlines instantly to readers without them refreshing the page.
Manual repeated requests cause delays and waste resources.
Long polling and Server-Sent Events let servers push updates instantly.
This makes apps faster, smoother, and more efficient.