0

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

  1. Is this a proper and reliable way to authenticate and manage Socket.IO connections?

  2. Will this fix the reconnection issues, or should I consider a different approach?

  3. 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)?

  4. 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!

2
  • Sorry for my note, but I just had a pretty bad time editing your post. Could you explain how could you screw up the formatting that badly? No problem and hard feeling, of course, I'm just curious: what happened? Please, never ever write those backslashes and don't try to escape characters. This is plain text or Markdown. Please check up your post after the approval of my changes and edit it more, if you have to. Commented Dec 9, 2025 at 18:23
  • You are very welcome. Commented Dec 9, 2025 at 18:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.