I am using socket.io for my chat application and completed both the implementation on both the backend and frontend. Recently I started to horizontally scale the system, integrated adapters and it all works fine, but I tried to implement the Connection state recovery so that the connected users can still use the system when the connected server crashes. Reference : https://socket.io/docs/v4/connection-state-recovery#disclaimer
const io = new Server(server, {
pingTimeout: 60000,
cors: {
origin: '*'
},
connectionStateRecovery: {
// the backup duration of the sessions and the packets
maxDisconnectionDuration: 2 * 60 * 1000,
// whether to skip middlewares upon successful recovery
skipMiddlewares: true
}
});
This is my new server configuration, but as I added the connectionStateRecovery property, I am experiencing an issue as the socket.emit(event, payload); which used to work fine is now sending the payload as an array. Even if the value of the payload is an object,
{
username: '_id',
online: true,
lastSeen: '2024-07-17T07:36:19.295Z'
}
at the client side it is received as an array of the payload and an id of sort( I believe that this is the id of the entry within the mongoDB collection setup for the adapter ), like:
[
{
username: '_id',
online: true,
lastSeen: '2024-07-17T07:36:19.295Z'
},
66979cdf69bcbed06f8f6518
]
Please reach out to me if you know why this is so, or is this the default behaviour, this issue is causing a blockage and I am not able to move forward.
Thank You, your cooperation is highly appreciated.
This is what I used as reference : https://socket.io/docs/v4/connection-state-recovery#disclaimer