This topic describes the concepts, benefits, and application scenarios of WebSocket.
WebSocket is a network protocol that enables interaction between a web browser and a web server over a persistent 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, persistent connection from a web browser to a server. This simplifies the message exchanges between clients and servers.
Many websites are using Ajax polling to facilitate push technologies. With the polling technique, the browser sends HTTP requests to the server at specific intervals (such as every second), and the server returns the most recent data to the browser of the client accordingly.
The disadvantage of this model is that the browser has to send an HTTP request to the server every time when an access request is received. HTTP requests may have a large header and a small payload. Sending such HTTP requests is a waste of bandwidth and other resources. The WebSocket protocol defined by HTML5 has the following benefits:
- Each message exchanged between the client and the server contains a very small header, which has a size of 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.
- The WebSocket protocol helps to reduce server resource and bandwidth usage and facilitate real-time communication.
|Live commenting||User A sends a live comment through a mobile phone and also wants to view live comments sent by other clients on the mobile phone. To meet this requirement, you can use WebSocket to push the live comments sent by other clients to the mobile phone of User A. User A can then view the live comments sent by other users.|
|Online education||On a one-to-many online education platform, the teacher can use WebSocket to push the notes and syllabuses edited on the teacher's client to the students' clients in real time.|
|Real-time quotes for financial products||The price of financial products such as stocks and gold changes rapidly. WebSocket enables the price of financial products to be pushed to clients around the world in real time to help traders make informed trading decisions.|
|Live sportscast||Live sportscast is the top concern for numerous sports lovers all over the world. WebSocket facilitates real-time updates in live sportscast to ensure the best viewing experience.|
|Video conferences||Video conferences are widely used in multiple scenarios. In a video conference, participants join the conference through multiple terminals. WebSocket helps to deliver real-time information to these participants.|
|Geolocation-based applications||An increasing number of developers are using the GPS feature of mobile devices to facilitate geolocation-based applications. If you have kept a record of the end user's location, for example, the user's movement trails recorded by an app, you can use WebSocket to collect more detailed data.|