Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round-trip time of AJAX v. Web Sockets

I would like to ask if I should expect some different round-trip time (sending some information to the server and recieving a response) when implemented with web sockets (message) compared to a standard HTTP GET. I assume that the web socket is already connected and the DNS is resolved.

As far as I understand it it would be different if GET would consist of multiple roundtrips in the underlying protocol, which I'm not sure of. Otherwise I would expect the same results.

like image 654
tillda Avatar asked Dec 01 '11 15:12

tillda


People also ask

Which is better AJAX or WebSocket?

However, Websocket push is far superior to AJAX (no need to re-authenticate or resend headers, no need for "no data" roundtrips, etc').

What is the difference between AJAX and WebSockets?

Ajax uses the HTTP Protocol and can send requests using POST/GET methods from Client to Server. WebSocket is itself a protocol to communicate between Client and Server, distinct from HTTP. In Ajax when you send a request , server sends response for that request and connection ends.

How much faster are WebSockets?

Plain WebSocket can be up to 3.7 times as fast to receive a message from the server compared to Socket.IO and 1.7 times as fast compared to SockJS. Plain WebSocket scales well in terms of response time and memory requirement with higher concurrency levels.

Are WebSockets faster than rest?

Fast Reaction Time If WebSockets are used, each user can both send and receive messages in real-time. WebSockets allow for a higher amount of efficiency compared to REST because they do not require the HTTP request/response overhead for each message sent and received.


2 Answers

WebSockets seem to have a lower round trip time. I ran some tests locally and on a remote server, averaging the trip times for 100 requests at a time:

Local:
WebSocket:   2.46ms
Ajax:        9.97ms

Remote:
WebSocket:  93.41ms
Ajax:      183.49ms

The tests were done with Node.js with express and socket.io on the server, and Chrome with socket.io's library on the client. The remote tests were run over a 3G connection.

Update: At home on a much lower-latency connection, the numbers are a bit different:

Websocket:  63.02ms
Ajax:       72.11ms

This suggests that latency has a larger effect on HTTP requests than on WebSocket connections, which is likely because HTTP has to make a couple more round trips to reestablish a connection for each request, as you mentioned.

like image 190
Joe Friedl Avatar answered Sep 18 '22 19:09

Joe Friedl


It depends on the initial scenario you are considering.

Example 1: You already have an HTTP 1.1 connection in place in one case and a WebSocket already established in the other case. In this scenario the round trip for both cases will be exactly the same, because in both cases you already have the TCP connection established and no further application-handshake is necessary. (Note: the quantity of data sent for the two cases will be different, but this impacts bandwidth, not latency, that is, round-trip time).

Example 2: You already have an HTTP 1.1 connection in place in one case (because perhaps you just downloaded the last image in your page) and no WebSocket opened in the other case. Well, in this scenario the round-trip time over HTTP will be lower than the round-trip time over WebSocket. The reason is that with HTTP you only need to send a TCP segment and receive a TCP segment (single round trip). With WebSockets you need to set-up the TCP connection and to perform the WS handshake, which involves a few round trips.

like image 42
Alessandro Alinone Avatar answered Sep 18 '22 19:09

Alessandro Alinone