There are lots of posts already about how to gracefully shutdown Node http servers:
Graceful shutdown of a node.JS HTTP server
Nodejs/express, shutdown gracefully
How do I shut down my Express server gracefully when its process is killed?
To also gracefully shutdown all open websocket connections, just add this to your shutdown handler:
for (const client of wss.clients) {
client.close(1001, 'Server shutdown');
}
wss.close();