This topic describes the concept, benefits, and application scenarios of WebSocket.
WebSocket is a new network protocol that enables interaction between a web browser and a web server over a persistent Transmission Control Protocol (TCP) connection. WebSocket supports full-duplex communications that allow the server to actively send data to the client. Therefore, WebSocket requires only one handshake to establish a bi-directional, full-duplex, and persistent connection between the browser and the server. This simplifies the data exchanges between the client and the server.
The disadvantage of this model is that the browser has to continuously send requests to the server. HTTP requests may have a large header and a small payload. The HTTP requests of this type result in a waste of bandwidth and other resources.
The WebSocket protocol that is defined by HTML5 has the following benefits:
- Each message that is exchanged between the client and the server contains a small header. The size of the header is about 2 bytes.
- Instead of returning data after receiving a request from the browser, the server actively pushes data to the browser when new data is available.
- When a WebSocket connection is established, the browser continuously communicates with the server.
The WebSocket protocol helps you minimize the usage of server and bandwidth resources, reduce the amount of the transferred data, and facilitate real-time communication.
|Live commenting||User A sends a live comment through a mobile phone and wants to use the mobile phone to view the live comments that are sent by other clients. To meet the requirements, you can use WebSocket to push the live comments that are sent by other clients to the mobile phone of User A. Then, User A can view the live comments that are sent by other users.|
|Online education||When a teacher offers courses to students online, the teacher can use a client to send data to the clients of the students in real time based on WebSocket communication. The examples of the sent data include notes and outlines.|
|Real-time quotes for financial products||To handle the fluctuating prices of financial products, such as stock and gold, WebSocket pushes the up-to-date prices to the clients of global traders in real time. This helps the traders make informed decisions at the earliest opportunity.|
|Live sportscast||Live sportscasts are the top concern for a large number of sports lovers all over the world. WebSocket allows for real-time updates in live sportscasts to ensure optimal user experience.|
|Video conferencing||Video conferencing is widely used in diverse scenarios. In a video conference, WebSocket helps to deliver real-time information to participants who join the conference through multiple ends.|
|Location-aware applications||An increasing number of developers apply the GPS feature of mobile devices to location-aware applications. You can use WebSocket to keep tracking the location of a user. For example, your WebSocket-based application can record the movement trails of the user. This allows you to collect more details about the user.|