0

i am writing a small websocket server and now I want to gracefully shutdown the service. I expect before closing that all active connections are handled until they are ready.

I am using NodeJs and wss to handle the websocket connections

Anyone an idea?


  • I really don't get it, what is your question ? – nerap Jun 18 '21 at 20:35
  • [This what you're looking for?](https://stackoverflow.com/questions/18874689/force-close-all-connections-in-a-node-js-http-server) – selfagency Jun 18 '21 at 20:36
  • https://www.npmjs.com/package/wss is 3 years old. consider updating to another maintained library when you can. – Raphael PICCOLO Jun 18 '21 at 20:38
  • How can i gracefully shutdown wss websocket connections when terminating a nodejs backend service? So when updating a service with a new version I want to be sure that all connections are handled until they close there connection. – SaarlandProgger Jun 18 '21 at 20:39
  • the graceful shutdown your are talking about is related to socket optimisation ? or to allow zero downtime ? i need a sample of your use case to answer anything – Raphael PICCOLO Jun 18 '21 at 20:40
  • zero downtime is much more that only nodejs code. it greatly depends on your infrastructure. you use docker ? – Raphael PICCOLO Jun 18 '21 at 20:43
  • I am using rancher kubernetes cluster with docker. When deploying a new version of my websocket service I want to guarantee that every connection is handled until they are finish to have a zero downtime... I have a game with active connections and I dont want to kick the player out of the game – SaarlandProgger Jun 18 '21 at 20:46

1 Answers1

0

i would use :

  • traefik to do load balancing
  • docker swarm / kubernetes to handle deployments and rolling updates of your project
  • have a well written nodejs code.

i dont know particularly wss, but the concept is to increment a work count on the server when people ask for work and decrement it when the server responds. then you can confidently exit the server as soon a the count reaches 0.

when you want to close the server, just stop accepting connections (and work) and exit server as soon as the count reaches zero.

the load balancer needs to stop routing requests to the node you want to stop before you start stopping it. you can use healthchecks for that.

When you configure zero downtime perfectly AND you have a perfect network you dont need to do the following, but i would also add a "retry on failure" on the client side.

Raphael PICCOLO
  • 2,095
  • 1
  • 12
  • 18