Словник

WebSocket

WebSocket enables two-way communication between a client (e.g., a web browser) and a server (which can be either an HTTP server or another WebSocket server).

What Is a WebSocket?

A WebSocket is a two-way communication channel that stays open, hence the name. A WebSocket connection is initiated by a process named handshake where a client sends a HTTP request to the server, which establishes the session. 

After the handshake, both client and server can send arbitrary data, with client-initiated messages acknowledged by the server by sending messages back to the client. Because of this full-duplex communication channel, control flow between client and server is returned to one-way messaging from two-way messaging when using HTTP cookies.

The important part is that it's an open connection between the browser and the server, and it transmits data in both directions.

WebSocket can be used for different applications, including chat rooms, poker games, stock tickers, multiplayer games, video streaming services, and many more. The only thing you need to do is open up a port in your firewall and configure your web server to use it.

Tons of data is transferred between the browser and the server, and sometimes it's just easier to open a connection and keep it open until the work is done. This is especially true when dealing with large amounts of data or data that changes frequently. The HTML5 WebSocket API allows you to do just that.

Taking advantage of WebSockets can allow web applications to function more efficiently by reducing latency, which in turn can translate into better user experiences. 

Here’s how different use cases of WebSocket enable a better user experience:

Real-time web applications: WebSockets are bi-directional communication channels over a single TCP socket. Unlike HTTP, a WebSocket connection can be kept alive indefinitely. They are intended to be used for two-way communication between client and server where a large amount of data needs to be sent from the server to the client without losing any data or waiting for the client to request more data.

Trading websites rely on WebSocket channels, so users are always aware of the fluctuations. These website’s backend servers keep pushing data to the client end on the same open channel.

Gaming applications: One of the significant use cases of WebSocket is gaming applications, where it can be used to enable real-time scenarios. It maintains a persistent connection between the client and the server. This allows for real-time data transmission without refreshing the user interface. The UI won’t need to make another connection to receive data.

Chat applications: When messages need to be broadcasted among users, WebSocket is the ideal protocol. Chat applications establish the connection using WebSocket and reuse the same open connection to send messages.