If you've ever used a server-side language and a database to build a web chat room, you'll want to know the difference between the websockets and the traditional way of implementation.
Traditional chat rooms are usually implemented using the polling method. The client periodically asks the server if there is an update. The server replies to the client's updated data or is not updated. However, the traditional approach has the following problems. Only the client actively asks the server to obtain the updated data from the server. This means that the updated data is periodically delayed and the server is not responding in a timely manner. If we want to improve this problem by reducing the polling process, we need more bandwidth because the client needs to send requests to the server without interruption.
Shows the request between the client and the server. It indicates that many useless requests have been sent, but there is no new data in the content that the server replies to the client.
Long polling is a better polling method. The client sends a request to the server and waits for a reply. Instead of the traditional polling method, the server replies with "No update" and the server replies only when it needs to push the information to the server. In this approach, the server can push information to the client whenever an update is available. Once the client receives a response from the server, it builds another request and waits for the next server to push. Displays a long poll, the client asks for updates, and the server replies only when there is an update:
In the WebSockets method, the number of requests is significantly less than the polling method. This is because the connection between the client and the server is permanent. Once the connection is established, the client or server side sends the request only when the information is updated. For example, the client sends information to the server when it wants to update the information to the server. The server sends a message to the client only if it needs to be pushed to the client to update the data. In the connection, there is no useless request sent. Therefore, only a smaller bandwidth is required. Shows the WebSockets method:
The difference between websockets and rotation implementations