WebSocket
noun
A WebSocket is an open, two-way connection between a client and a server. Both sides can send messages on their own initiative through that connection.
The dashboard receives new measurements through a WebSocket and updates the chart immediately.
A WebSocket lets a client and server exchange messages during an active session. The client might be a browser or mobile app. Once the connection opens, neither side needs to start a new request for every message. The server can also send a message as soon as new information is available.
How is a WebSocket connection established?
The client starts with an HTTP request asking to switch to the WebSocket protocol. If the server accepts, the underlying network connection stays open. Client and server can then send messages independently until one closes the connection or it is lost.
The application defines what those messages mean. They can contain text or binary data, such as a new chat message, a courier's current position or an updated status. After losing a connection, the application usually needs to reconnect and decide whether missed data must be retrieved.
When should you use a WebSocket?
WebSockets suit applications where data changes frequently in both directions, such as chats, collaborative documents, live dashboards and multiplayer games. The open connection avoids the client repeatedly asking whether anything is new, as with polling.
For infrequently changing data, an ordinary Glossary · In briefAPIAn API is a defined way for software to exchange data or call functions in other software without needing to know how that software works internally.Read more may be simpler. If only the server needs to send updates to the client, a one-way solution may suffice. WebSockets also require management: the server must handle many concurrent connections, connection loss, access control and Glossary · In briefpeak loadPeak load is the temporarily high demand on a system when many users, requests or tasks need capacity at the same time.Read more.