You still need to send requests from your computer to the website's server and back and forth. How can websockets make it so much faster?
2 Answers
WebSocket is a extension for HTTP. For low-latency communication Web Sockets are better.
Also check this article
How can websockets make it so much faster?
To establish a WebSocket connection, the client and server upgrade from the HTTP protocol to the WebSocket protocol during their initial handshake, as shown in the following example:-
GET /text HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: www.websocket.org
HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
…
Once established, WebSocket data frames can be sent back and forth between the client and the server in full-duplex mode. Both text and binary frames can be sent full-duplex, in either direction at the same time. The data is minimally framed with just two bytes. In the case of text frames, each frame starts with a 0x00 byte, ends with a 0xFF byte, and contains UTF-8 data in between. WebSocket text frames use a terminator, while binary frames use a length prefix.
Web Sockets represents the next evolution of web communications—a full-duplex, bidirectional communications channel that operates through a single socket over the Web. HTML5 Web Sockets provides a true standard that you can use to build scalable, real-time web applications. In addition, since it provides a socket that is native to the browser, it eliminates many of the problems Comet solutions are prone to. Web Sockets removes the overhead and dramatically reduces complexity.
Latency comparison:-
Summary:-
Web Sockets provides an enormous step forward in the scalability of the real-time web. As you have seen in this article, HTML5 Web Sockets can provide a 500:1 or—depending on the size of the HTTP headers—even a 1000:1 reduction in unnecessary HTTP header traffic and 3:1 reduction in latency. That is not just an incremental improvement; that is a revolutionary jump—a quantum leap!
-
1the downside of websockets is that they do not follow the http semantic for the request/response making protocols like SPDY and the forthcoming HTTP/2.0 more attractive in many peoples eyes since they offer many of the same benefits yet retain that semantic (spot on example of the difference though, so +1 from me Rahul) Oct 3, 2013 at 21:32
-
@jessemcconnell:- Very nice point Sir! Also to add one more point from the same link although the Web Sockets protocol is ready to support a diverse set of clients, it cannot deliver raw binary data to JavaScript, because JavaScript does not support a byte type. Therefore, binary data is ignored if the client is JavaScript—but it can be delivered to other clients that support it. Oct 3, 2013 at 21:34
-
while we are on the topic and since 'Comet' is in the tags for the question, I generally recommend using something like cometd.org for your communication bus instead of say websockets directly since it can automatically choose the best protocol based on what the browser and server support, http/2, spdy, ws, http/1.1 long polling, even http/1.0 polling and also layers on support for reconnection, ensuring message ordering, etc etc Oct 3, 2013 at 21:39
-
All modern browsers have binary data support in Javascript via Blobs and Typed Arrays (both can be sent and received over WebSockets).– kanakaOct 3, 2013 at 23:07
-
@jessemcconnell The cometd homepage says describes it as "us[ing] a Ajax Push technology pattern known as Comet". Comet is used to describe various long-polling techniques, of which websockets are not one. Are you perhaps thinking of
socket.io
? That does do what you described. Oct 4, 2013 at 3:10
Websocket is distinct from http. As http is half-duplex which means communication can be from either sides(client and server) but not at same time. Websocket is said to be faster than http because it provides full duplex communication. So, both client and server can communicate at the same time.