@@ -183,6 +183,64 @@ app.listen();
183183console .log (' Server running on http://localhost:3000' );
184184```
185185
186+ #### ConnectionManager API
187+
188+ The ` ConnectionManager ` class provides the following methods and properties:
189+
190+ - ** Methods** :
191+ - ` setServer(server: Server) ` : Associates the connection manager with a server instance
192+ - ` addConnection(socket: Socket) ` : Registers a new socket connection for tracking
193+ - ` removeConnection(socket: Socket) ` : Removes a socket from tracking when it closes
194+ - ` getStats() ` : Returns statistics about connections (` IConnectionStats ` )
195+ - ` closeAllConnections(gracePeriod?: number) ` : Gracefully closes all active connections
196+ - ` on(event: ConnectionEvent, listener: Function) ` : Subscribes to connection events
197+ - ` off(event: ConnectionEvent, listener: Function) ` : Unsubscribes from connection events
198+
199+ - ** Events** :
200+ - ` CONNECTION_ADDED ` : Fired when a new connection is established
201+ - ` CONNECTION_REMOVED ` : Fired when a connection is closed
202+ - ` CONNECTION_ERROR ` : Fired when a connection encounters an error
203+ - ` ALL_CONNECTIONS_CLOSED ` : Fired when all connections have been closed
204+ - ` SERVER_LISTENING ` : Fired when the server starts listening
205+ - ` SERVER_CLOSED ` : Fired when the server is closed
206+
207+ - ** Statistics** :
208+ - ` activeConnections ` : Number of currently active connections
209+ - ` totalConnections ` : Total number of connections since server start
210+ - ` connectionErrors ` : Number of connection errors encountered
211+ - ` uptime ` : Server uptime in milliseconds
212+
213+ #### Graceful Shutdown Pattern
214+
215+ For production applications, implementing a graceful shutdown pattern is recommended:
216+
217+ ``` typescript
218+ // Graceful shutdown handler
219+ const gracefulShutdown = async (signal : string ) => {
220+ console .log (` ${signal } received, starting graceful shutdown ` );
221+
222+ // Step 1: Stop accepting new connections (optional)
223+ server .close ();
224+
225+ // Step 2: Allow existing connections to finish (with timeout)
226+ console .log (' Closing remaining connections...' );
227+ await app .connectionManager .closeAllConnections (10000 ); // 10 second grace period
228+
229+ // Step 3: Close the server completely
230+ console .log (' Shutting down server...' );
231+ await app .close ();
232+
233+ console .log (' Shutdown complete' );
234+ process .exit (0 );
235+ };
236+
237+ // Register shutdown handlers
238+ process .on (' SIGTERM' , () => gracefulShutdown (' SIGTERM' ));
239+ process .on (' SIGINT' , () => gracefulShutdown (' SIGINT' ));
240+ ```
241+
242+ This pattern ensures that your server can handle restarts and deployments without dropping active connections.
243+
186244## Examples
187245
188246Check out the [ examples] ( /example ) directory for more detailed usage examples:
@@ -199,6 +257,7 @@ For detailed documentation, see the [docs](/docs) directory:
199257- [ Request Lifecycle Hooks] ( /docs/hooks.md ) - In-depth documentation of the hooks system
200258- [ Content Type Handling] ( /docs/content-types.md ) - Working with different content types
201259- [ Error Handling] ( /docs/error-handling.md ) - Guide to handling errors at different levels
260+ - [ Testing Guide] ( /TESTING.md ) - Comprehensive guide to testing the framework
202261
203262## Project Structure
204263
@@ -210,6 +269,7 @@ yinzerflow/
210269│ ├── javascript/ # JavaScript example
211270│ └── typescript/ # TypeScript example
212271├── package.json # Package configuration
272+ ├── TESTING.md # Testing documentation
213273└── README.md # This file
214274```
215275
0 commit comments