I have some event listeners attached to a socket on the server side. When the client connected to that socket is disconnected (on page refresh for example), it reconnects with a new socket on the server side different from the one it was using before it disconnected. So all event listeners attached to the old socket are lost. Is there a way to keep event listeners even after reconnection ? Thanks !
Client:
socket.on('connect', () => {
// Do some stuff
});
Server:
io.on('connection', (socket) => {
const CUSTOM_EVENT = 'CustomEvent';
onTimeRunFunctionThatAttachesListnerToAnEvent(socket, CUSTOM_EVENT);
console.log(`Client connected to socket id '${socket.id}'`);
console.log(`Nb of listners to event ${CUSTOM_EVENT} : ${socket.listenerCount(CUSTOM_EVENT)}`);
socket.on('disconnect', (reason) => {
console.log('client disconnected');
});
});
Logs
Client connected to socket id '9FKJ152mdOLaHcC2AAAB'
Nb of listers to event 'CustomEvent' = 1
client disconnected
Client connected to socket id 'ulqq3yvjQflXLyHTAAAF'
Nb of listers to event 'CustomEvent' = 0