Best way to find a free server (from a server pool) #4331
-
First of all, thank you for the great repo, it took a while for me to understand but I made things work until this point. Here is my situation:
The client should try the pool starting with the first server, if busy it will try the next one until all are scanned (I'll convert it to random). I could implement this to the point where a request to the second server is made (because the first one is "busy"), and the server accepts the client but the callbacks on the client do not get triggered. I'm rather new to socket.io and I think I have a basic misunderstanding of how the callbacks are connected. Any insight on the issue I have or any pointer for another method is much appreciated. Here is some simplified code for the client (which runs on a button click): const serverConnect = () => {
const serverPool = [.... here are the server adresses inc ports ...];
let serverPoolInx = 0;
let socket: Socket;
const tryConnect = (server: string) => {
return io(server, {
autoConnect: true,
forceNew: true,
transports: ["websocket"],
timestampRequests: true,
reconnection: true,
reconnectionAttempts: RECON_TRIALS,
reconnectionDelay: RECON_DELAY,
reconnectionDelayMax: RECON_MAX,
rejectUnauthorized: false,
timeout: TIMEOUT,
secure: true,
});
};
socket = tryConnect(serverPool[serverPoolInx]);
const reTry = () => {
unSuccessfulCount++;
if (unSuccessfulCount < serverPool.length) {
serverPoolInx = (serverPoolInx + 1) % serverPool.length;
socket = tryConnect(serverPool[serverPoolInx]);
} else {
console.log("CONNECT-FAILED-FOR-ALL-SERVERS");
}
};
socket.io.on("reconnect_failed", () => {
console.log("CONNECT-FAIL:", serverPool[serverPoolInx]);
reTry();
});
socket.on("full", () => {
console.log("SIO: Server full!");
socket.disconnect();
setConnectedStatus(false);
reTry();
});
socket.on("accept", () => {
console.log("SIO: Accept!");
setSocket(socket); // make it global
setConnectedStatus(true);
// other stuff come here
});
socket.on("connect", () => {
console.log("CONNECT-SUCCESS:", serverPool[serverPoolInx]);
setConnectedStatus(true);
});
// other socket.on stuff As I said before, on the second server's connection try, the socket.on calls get not executed. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I step-debugged it watching "socket" instance. Whenever I call io(newadress...) the callsbacks are no more attached. Should I use the Manager in my case? |
Beta Was this translation helpful? Give feedback.
-
I moved the retry loop one level up and connected it to a timer set to timeout. This temporarily solved it. It has to wait for that timeout period though (I use 3 trials & 5 sec. timeout). Probably I need to use a Promise with the io-connect, let's figure this out now... Also searching for a more natural way... |
Beta Was this translation helpful? Give feedback.
-
I think the issue is that the You need to either:
const tryConnect = (server: string) => {
return io(server, {
autoConnect: true,
forceNew: true,
transports: ["websocket"],
timestampRequests: true,
reconnection: true,
reconnectionAttempts: RECON_TRIALS,
reconnectionDelay: RECON_DELAY,
reconnectionDelayMax: RECON_MAX,
rejectUnauthorized: false,
timeout: TIMEOUT,
secure: true,
})
.on("full", () => {
console.log("SIO: Server full!");
socket.disconnect();
setConnectedStatus(false);
reTry();
})
.on("accept", () => {
console.log("SIO: Accept!");
setSocket(socket); // make it global
setConnectedStatus(true);
// other stuff come here
});
};
const socket = io(serverPool[0], {
autoConnect: false,
// ...
});
const tryConnect = (server: string) => {
socket.io.uri = string;
socket.disconnect().connect();
}; Minor nitpick: I'd use a middleware instead of the accept/full events: io.use((socket, next) => {
if (isFull) {
next(new Error("full!"));
} else {
next();
}
}); Reference: https://socket.io/docs/v4/middlewares/ |
Beta Was this translation helpful? Give feedback.
I think the issue is that the
socket
reference is updated, but the listeners are still attached to the previous socket instance.You need to either:
tryConnect()
method