Course contentsShow
← Node.js
Lesson 1162 of 1,98010. Real-time Communication and WebsocketsFree lesson

Managing Lifecycle Hooks in Frameworks

You will integrate socket lifecycle events with existing framework startup and shutdown routines.

In this lesson, you will learn how to synchronize your WebSocket server’s lifecycle with your Node.js application startup and shutdown routines to ensure graceful transitions.

When building real-time systems, your WebSocket server is a stateful entity that cannot simply be "killed" without potentially losing inflight messages or leaving clients in a hung state. To manage this, you must hook into your framework’s initialization and destruction events—such as onModuleInit or beforeShutdown—to orchestrate the socket connection flow.

Orchestrating Startup and Shutdown

At startup, you must ensure that your socket gateway is ready to receive connections only after your internal services (like Redis or database pools) are fully primed. Conversely, during shutdown, your priority is to stop accepting new connections and disconnect existing ones cleanly, allowing clients to initiate their reconnection logic (which we previously discussed in our lesson on handling reconnection storms).

Here is how you might structure this within a standard framework hook:

class SocketGateway {
  // Triggered when the framework boots
  async onModuleInit() {
    await this.server.listen(3000);
    console.log('WebSocket Gateway is online.');
  }

  // Triggered when the process receives SIGTERM
  async beforeShutdown() {
    console.log('Shutting down WebSocket gateway...');
    
    // 1. Stop accepting new connections
    this.server.close(); 
    
    // 2. Notify clients to reconnect to a healthy instance
    this.server.emit('system-shutdown', { retryIn: 5000 });
    
    // 3. Forcefully terminate remaining connections after a timeout
    await new Promise(resolve => setTimeout(resolve, 5000));
    this.server.disconnectSockets();
  }
}

Why Lifecycle Matters

By binding these events, you transform your WebSocket server from an independent process into a managed component of your application. If you fail to handle these hooks, a process restart might lead to "ghost connections" that attempt to communicate with a database pool that has already closed its connections. By explicitly managing the socket lifecycle—closing the server, broadcasting departure notifications, and awaiting cleanup—you ensure that your distributed architecture remains stable and predictable during deployments.

Key Takeaway: Always pair your socket server startup with an explicit teardown sequence to prevent connection leaks and ensure graceful transitions during process restarts.