HTTP is like walkie-talkie; data transmission can happen from only one direction at a time. WebSocket is like a telephone; data transmission can happen from both direction at the same time.
Walkie-Talkie / HTTP |
Telephone / WebSocket |
HTTP
In HTTP both ends can transfer data but only one at a time. HTTP clients (like browsers) asks for data from the server by sending the request and then the server parses the request, understands it and responds appropriately by transferring response data back to the client. HTTP was designed to be half-duplex.
HTTP was not designed to be highly interactive. This means if the server has some new data to push to a client, it waits for client to ask for it. The server has no way to just transfer the data as and when it wishes. There are work-arounds to overcome this limitation like polling and long-polling. In polling, the client keeps making request to the server in regular intervals and server responds back if there is any data available, otherwise, the request gets ignored by the server. In long-polling client makes a call and server keeps connections open and waits for a fixed time for data to be available and then responds back to the client (or times out). Long polling is also known as Comet. Clearly, both alternatives for interactive communication (from both-ends) are not efficient and not ideal of real-time applications like chat.
WebSocket
Above limitations clearly, suggest that there was a need for more reliable bi-directional (full-duplex) communication between two endpoints (in web-world known as client and server). WebSockets allows bi-directional communication from both ends at the same time. This means, even servers can create connection and push data to a client. It enables more interactive and real-time communication between client and server. WebSocket packets are lightweight compared to HTTP packets and in a single connections client/server can push multiple messages. So, it's more efficient than HTTP.
Note: Both HTTP, as well as WebSockets, use TCP.
References:
http://www.html5rocks.com/en/tutorials/websockets/basics/
References:
http://www.html5rocks.com/en/tutorials/websockets/basics/
No comments:
Post a Comment