Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO client library slow loading

enter image description here

I start my server, and refresh the page in a browser, which then takes >2s to load the JS resource. If I then reload the page in any browser, it loads quickly.

This is only happening the first request after the server has been started. I suppose it has something to do with it putting together the JS file the first time, and then after that it is cached on the server.

Can anything be done to cut down this time?

I have tried both with and without the production settings (gzip, minify etc).

Client code:

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
</script>

Server code:

var express = require('express'),
    expressServer = express.createServer(),
    socketServer = require('socket.io').listen(expressServer);

expressServer.listen(1337);
like image 552
Znarkus Avatar asked May 31 '12 06:05

Znarkus


1 Answers

There is currently a bug in socket.io that is causing this. Make sure you do NOT have this set and it should load MUCH faster:

io.set('browser client gzip', true);          // gzip the file

The first call to load socket.io.js will try to compress it and store it in memory. You will run into these bugs:

  • https://github.com/LearnBoost/socket.io/issues/984
  • https://github.com/LearnBoost/socket.io/issues/932

You can get some speed increase by using the minified version and allowing caching until this is fixed:

io.set('browser client minification', true);  // send minified client
io.set('browser client etag', true);          // apply etag caching logic based on version number
like image 157
bendiy Avatar answered Sep 30 '22 01:09

bendiy