Suggested way to deploy major socket.io upgrade (1

2019-07-23 07:22发布

问题:

As socket.io 2.0 is not backward-compatible (https://socket.io/blog/socket-io-2-0-0/) I'm curious what is the suggested procedure of production upgrade.

The problem is there are many people connected to a server when deployment takes place, and after deployment is completed they are still most likely using old socket.io client trying to reconnect.

Backward-incompatibility in my case (1.4.5 -> 2.0) turns out to be pretty troublesome as old client keeps sending handshake requests at an insane rate, and a new server keeps shoving them off resulting in huge cpu and ram loads on server.

What's suggested strategy in this case (preferably long-term, i.e. taking care of future updates too)? It's not first socket.io backwards-incompatible release and I have a strong feeling there already good practices at work.

Thank you in advance, Vasiliy Naumov

回答1:

I'll probably gonna resort to adding a "connect_error" listener on a socket, which is triggered with parser error when 1.4.5-compatible socket.io client tries to connect to 2.0 socket.io server, from there I'll reload the page and let the browser to load fresh javascript including a newer socket.io client like so:

socket.on('connect_error', function (error) {
  if (error.code === 'parser error') {
    // potential socket.io protocol update
    socket.io.reconnection(false); // stop reconnection attempts
    socket.disconnect();

    // additional check in case there is no actual socket.io client update, but rather some other reason for parser error being thrown
    var upgradeCheckedAlready = window.sessionStorage.getItem('socket.io-upgrade-check');
    if (!upgradeCheckedAlready) {
      window.sessionStorage.setItem('socket.io-upgrade-check', 1);
      window.location.reload();
    }
  }
})