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

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


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


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");});