Suggested way to deploy major socket.io upgrade (1.4.5 to 2.0) to a production environment

﹥>﹥吖頭↗ 提交于 2019-12-11 05:39:52

问题


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


来源:https://stackoverflow.com/questions/44676717/suggested-way-to-deploy-major-socket-io-upgrade-1-4-5-to-2-0-to-a-production-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!