In a chat app, your friend's message appears the moment they send it, without you reloading the page. Yet usually the browser asks first, and the server only answers.
Asking again and again
If the server can only answer, the browser has to ask again and again, "any new message?", every few seconds. Most answers are "not yet", and every question costs time and data. If the gap between two questions is long, messages appear late.
A line that stays open
A WebSocket opens one line (a connection) between the browser and the server, then leaves that line open. Over that line the browser and the server can both send messages at any time, so the server can send a message the moment there is one, without being asked. WebSockets are used for chat, live match scores and games with many players.
TL;DR: Usually the browser asks and the server only answers. A WebSocket opens a two-way line that stays open between the browser and the server, so the server can send a message first, without being asked.
⚡ Asking again and again
Guess first: the chat app on your phone can only ask the server "any new message?" every few seconds. Your friend sends a message right after the last question. When does that message appear on your phone?
Only after the next question. The longer the gap between two questions, the later the message appears.
Meaning: Usually the browser asks and the server only answers. So the browser has to ask again and again, "any new message?". Most answers are "not yet", and every question costs time and data.
⚡ A line that stays open
Meaning: A WebSocket opens one line (a connection) between the browser and the server, then leaves that line open. The browser and the server can both send messages at any time, so the server can send a message the moment there is one, without being asked. WebSockets are used for chat, live match scores and games with many players.