Menu

WebSockets tutorial: how they work and when to use them
πŸ“°

WebSockets tutorial: how they work and when to use them

DEV Community: flaskΒ·Hassan AzizΒ·3 days ago
#AZcbg1OL
#devtoflask#devto#dev#server#websocket
Reading 0:00
15s threshold

The short version

A WebSocket is a connection that stays open between the browser and the server, so either side can send a message at any time.

HTTP is request and response: the server can only answer when the client asks. WebSockets are events, and neither side has to reply.

A WebSocket starts as an HTTP request with an Upgrade header, and the same connection then carries WebSocket messages.

Use WebSockets when the server has to push updates without being asked, like chat messages or progress from a long-running job. Keep HTTP for everything else.

Use wss:// rather than ws://, and pick it from the page's own protocol so the same code works locally and in production.

A WebSocket is a connection between a browser and a server that stays open, so either side can send a message whenever it wants. Plain HTTP can't do that. With HTTP the server only ever answers requests, so if the client doesn't ask, the server has no way to tell it anything.

That one difference decides when you need WebSockets.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.