I’m currently working on a logistics app that manages goods transportation in my area. The tech stack includes React Native for the mobile app, Node.js / Express.js for the backend, MongoDB as the database and socket.io for real-time data update.
I’m facing a consistent issue with Socket.IO, which I’m using for real-time updates. Sometimes the socket disconnects and fails to reconnect on the client side. As a result, the app does not always receive the latest data. This also happens when the backend is running in an autoscaled environment.
I initially stored the socket IDs in memory and later tried Redis as well, but I still occasionally encounter reconnection failures. I was passing the userId through query parameters, as shown below:
socket = io(SOCKET_URL, {
transports: ['websocket'],
forceNew: true,
reconnection: true,
query: {
userId: <userID>,
role: 'customer',
},
reconnectionAttempts: Infinity,
reconnectionDelay: 1000,
});
As a temporary workaround, I shifted to using notifications to trigger data updates. However, this doesn’t provide a smooth real-time experience—sometimes notifications arrive late or out of order, causing data to become inaccurate.
To rebuild this more reliably, I’m considering switching from query parameters to Socket.IO auth using a token:
const socketOptions = {
transports: ['websocket'],
auth: {
token,
},
reconnection: true,
reconnectionAttempts: Infinity,
reconnectionDelay: 1000,
reconnectionDelayMax: 10000,
randomizationFactor: 0.5,
timeout: 20000,
autoConnect: true,
forceNew: false,
};
socket = io(Config.SOCKET_BASE_URL, socketOptions);
Before restructuring everything, I’d like some guidance:
My questions
Is this a proper and reliable way to authenticate and manage Socket.IO connections?
Will this fix the reconnection issues, or should I consider a different approach?
Are there best practices for building a 100% accurate and stable real-time update system for mobile apps, especially when using autoscaling (multiple backend instances)?
If someone has experience restructuring such an architecture, could you please share suggestions or recommended patterns?
I’d like to remove the dependency on notifications for real-time updates.
Any advice or insight would be greatly appreciated.
Thank you!