Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket connection setup takes a relatively long time - is this normal?

It seems to be taking about 1 second to set up a websocket connection when the client and server are both running locally on my machine. This seems like quite a long time to me - is it? Could anyone compare this with their own experiences?


Details:

I have a websocket request like this on the client (Chrome 25, Windows7 64):

this.ws = new WebSocket('ws://' + host + ':' + port);

On the server side I have Node with ExpressJS running and einaros/ws handling the upgrade and WS connection.

I'm timing on the client, from just before the new WebSocket() to the ws.onopen event.

like image 532
UpTheCreek Avatar asked Feb 28 '13 12:02

UpTheCreek


People also ask

Why is WebSocket slow?

The underlying websocket transport is TCP, and this topic of coordinating different rates of sending and receiving is called flow control (it is a feature of TCP layer), and this specific problem is known as "slow consumer" (and is common when sending out very high amounts of data over TCP, such as financial market ...

How long do WebSocket connections last?

However, the connection between a client and your WebSocket app closes when no traffic is sent between them for 60 seconds.


2 Answers

I found a clear answer to this question. Apparently, using localhost causes the browser to try to connect to ipv6 first then fallback to ipv4 after a 1 second timeout. The problem is solved by using 127.0.0.1 because it will try to connect using ipv4 in the first place.

Source

like image 193
Jean-Philippe Leclerc Avatar answered Oct 09 '22 19:10

Jean-Philippe Leclerc


That is not normal.

I am using Chrome 24 on Ubuntu with the following test code (just fire up Chrome Dev console and paste it in):

function test_ws(uri){
    start = new Date().getTime(); 
    ws = new WebSocket(uri);
    ws.onopen = function(){
        console.log("onopen of", uri, "in", (new Date().getTime() - start), "ms");
    };
}

Here are some average results I've gotten for various values of uri:

  • ws://localhost:6080: 20 ms (custom python based WebSocket server)
  • ws://localhost:6090: 3 ms (custom node.js + einaros/ws based WebSocket server)
  • ws://echo.websocket.org: 130 ms
  • wss://echo.websocket.org: 190 ms

So even using an encrypted connection to a public remote Websocket server is still less than one fifth of a second on average until the open event. The maximum time I saw was 250ms. For a local connection, the delay should really only be a few milliseconds.

My guess would be that you server setup is doing a bunch of processing before accepting the connection. Perhaps you are initializing a bunch of client data in the new connection handler?

Update:

Here is a simple einaros/ws based WebSocket server that gives 3 ms onopen response using the client test code above:

var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({port: 6090});
wss.on('connection', function(ws) {
    console.log("got connection");
});
like image 29
kanaka Avatar answered Oct 09 '22 19:10

kanaka