Member-only story

Exploring Short Polling, Long Polling, Server-Sent Events, and WebSockets

Atakan Demircioğlu
JavaScript in Plain English

In this article, we will try to summarize the real-time communication techniques between clients and servers.

What is Short Polling?

Short polling is a technique where the client sends requests to the server for updates at regular, short intervals, such as every few seconds.

const endpointUrl = 'https://example.com/api/updates';
const pollingInterval = 5000; // 5 secs

async function pollServer() {
try {
const response = await fetch(endpointUrl);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log('Update received:', data);
} catch (error) {
console.error('Error fetching updates:', error);
}
}
// Set up the interval to poll the server
setInterval(pollServer, pollingInterval);
pollServer();

In this short polling example, HTTP requests are sent every 5 seconds to the specified endpoint.

Useful for periodic data updates, system health checks, or feed systems.

What is Long Polling?

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Responses (2)

Write a response

Well articulated examples! "Webhooks" would also be a good addition to this list..

Server-Sent Events is a great technology! Also recently published a video which dives into the main building blocks of Server-Sent Events in Go.