Should the access modifier of the httpServer object be set to private? #4794
-
I have been trying to handle port conflicts when starting the socket.io server, so that I can attempt to use the next available port if the current one is already in use. However, due to the inability to access the httpServer property, I am unable to monitor the EADDRINUSE event and take appropriate action. Am I missing other ways to catch EADDRINUSE error in Standalone mode? https://github.com/socketio/socket.io/blob/main/lib/index.ts#L205 io = new Server(port, { serveClient: false })
// @ts-expect-error
io.httpServer.on('error', (error: NodeJS.ErrnoException) => {
if (error.code === 'EADDRINUSE') {
console.log(`Port ${port} is already in use`);
socketStart(port + 1);
} else {
console.error('An error occurred:', error);
}
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi! I think you should provide your own HTTP server: import { createServer } from "node:http";
import { Server } from "socket.io";
const httpServer = createServer();
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.on("error", () => {
// ...
});
httpServer.listen(3000); Reference: https://socket.io/docs/v4/server-initialization/#with-an-http-server |
Beta Was this translation helpful? Give feedback.
Hi! I think you should provide your own HTTP server:
Reference: https://socket.io/docs/v4/server-initialization/#with-an-http-server