Run app on newest versions of Socket.IO gratter than 1.0

406 Views Asked by At

I need some help to make a code writed to Socket.IO < 0.9 works with newers versions, like 1.3:

var port = 843;

var io = require('socket.io').listen( port );
io.enable('browser client minification');  // send minified client
io.enable('browser client etag');          // apply etag caching logic based on version number
io.enable('browser client gzip');          // gzip the file
//io.set('log level', 1);  

io.sockets.on('connection', function (socket) {
  socket.on('woot_send', function(op){
    socket.broadcast.emit('woot_receive', op);
    if( op.type == 'cursor-create' && Object.keys(io.connected).length == 1 )
      socket.emit('woot_receive', {type: 'contents-init', contents: "Sample initial content that might be coming from permanent storage..."});
  });
  socket.on('woot_save', function(contents){
    console.log(contents);
  });
});

It causes the error:

/home/celso/woot/node/app.js:4
io.enable('browser client minification');  // send minified client
   ^
TypeError: undefined is not a function
    at Object.<anonymous> (/home/celso/woot/node/app.js:4:4)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

Minification, etag and gzip were removed from socket.io since version 1.0.0. io.enable function does not exist anymore with the API and that is why it throws an error. SocketIO now provides a CDN with gzip compression and minified version. Use this to enable your clients to use the CDN.

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>

SocketIO when introduced 1.0.0, specified CDN delivery methods which are described here. Specifically, the CDN (provided by Google zopfli) provides high level of zip compression, proper support for caching and built-in SSL support.

The recommendations from SocketIO Author are: Originally listed in this closed Pull Request

  • /socket/socket.io.js is meant for development. It can work in production, of course, but it won't be optimized
  • The CDN provides all the more advanced features that would otherwise bloat our codebase (minification, compression, caching).
  • Ultimately, if opting not to use the CDN, it's best to use socket.io as part of a build system (like webpack or browserify), and introduce minification and compression for the entire build, not each component.

An old SO Question on the same topic exists here, but does not include the latest version from the CDN.

If you face issues other than io.enable, there is a guide to migrating from 0.9 to higher versions of SocketIO.

So, you should remove the lines using io.enable function from version 0.9 and enable the clients to use socket.io from the CDN listed above.