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.


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.

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:

WebSocket:   2.46ms
Ajax:        9.97ms

WebSocket:  93.41ms
Ajax:      183.49ms

The tests were done with Node.js with express and on the server, and Chrome with'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.

Need Your Help

How do I add style to content in JavaScript?

javascript fonts coding-style colors

Hi I have been working in a JavaScript file and my content has been working with phrases. Now I want to change the style of those phrases. The first function (see function swapFE) I want to change ...

Is there any “READPAST” in Entity framework 5?

sql entity-framework

Is there a way to use/issue the SQL Server READPAST table hint in an entity framework LINQ query?