Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·6 min read

The server took every position and stored none of them

The most dangerous bugs in a delivery app are the ones with no symptom on the device. A courier's phone sends its position, the marker moves, nothing shows an error, and meanwhile the server is not writing any of it down. The dispatcher ranks offers by the last stored position, so that courier slowly stops getting work and nobody can tell why. I designed Chiti's position reporting around that one failure, and then spent two commits in November closing the gaps it left.

Project
Chiti
Date
Sep 2026 and Nov 2026
Stack
React Native, Socket.IO, TypeScript
Commit
d4e80a9, 6331d17, 844fa5f

What broke

Before this work, each location fix took two channels: an ephemeral broadcast over the socket, and a durable HTTP write that the server used for its own decisions. In d4e80a9 (Sep 2026) I made the socket carry both jobs: no timer cadence, every fix reported, gated by distance moved, with HTTP kept only as the fallback for a headless relaunch of the app.

That change made the socket the durable channel for a position. Nothing followed it with an HTTP write any more. So "did the frame arrive" stopped being a good enough question.

The server persists a position only for a socket that authenticated at the handshake. A socket opened without a valid token is still allowed to send events and the server still relays the courier's marker to anyone watching, but it never writes the position down. From the phone, the two cases are identical. The courier sees a working app. The customer sees a moving marker. The stored position, the one the offer feed is ranked by, stays where it was.

What the code showed

The fix for the first half is an acknowledgement. Socket.IO lets you attach a callback to an emit and set a timeout for it, so the client can ask the server for an answer to each position report:

ts
// ask the server whether it actually stored this fix
socket.timeout(10_000).emit("update_location", frame, (err, res) => {
  stores = !err && res?.stored === true;
});

The answer is folded into one boolean, storesPositions. It starts optimistic, because starting pessimistic would send a redundant HTTP write on every connect for a case that should rarely happen, and the first acknowledgement corrects it within a fix or two. It is reset to optimistic on every new connection, because a new connection is a new handshake, and the handshake may now carry a token the previous one did not have. Any error counts as a refusal, including a timeout, so a backend too old to acknowledge at all is treated the same as one that says no.

The location code reads that flag. When the server is not storing, or when the socket is not live, positions go out over HTTP instead. I also made a point of not trusting Socket.IO's offline buffer. emit returns true for a socket that exists but is disconnected, because the frame is queued and flushed later. That is right for joining a room and wrong for a position, which is last write wins and would arrive out of order after a long outage. So there is a separate check for whether the socket is actually connected.

That solved detection and fallback. It did not fix the cause.

The fix

On 9 Nov 2026 I closed the loop in two commits, about half an hour apart.

The cause was a token that expired while the app was in the background. The auth callback only runs when the socket connects. A phone that was backgrounded, lost its connection, and came back with an expired token would reconnect once, hand over a stale token, and stay anonymous for the rest of the trip. Every acknowledgement said stored:false, every position took the HTTP fallback, and the live marker was never rebroadcast.

6331d17 (Nov 2026) handled the symptom. When an acknowledgement explicitly says stored:false, the client now disconnects and connects once, so the handshake reads the current token. It is rate limited to one cycle per 30 seconds, and it does nothing when there is no session, so a genuinely signed-out user cannot make the socket spin. In the meantime the HTTP fallback is also what refreshes the token.

844fa5f (Nov 2026) handled the cause. The token store now notifies listeners whenever the access token is written, whether from a refresh, a sign in or a clear. The socket remembers which token its handshake used, and when the store holds a different one it cycles the connection. The notification is debounced by 500 milliseconds so a burst of writes, such as a rotation followed by a sign in, reconnects once. I also added a handler for an auth_required notice from the server, so a refused room join triggers the same remedy.

  1. 01

    Token expires

    the app is backgrounded and the socket stays up

  2. 02

    Position is sent

    with an acknowledgement and a 10 second timeout

  3. 03

    Server answers

    stored:false, because the handshake was anonymous

  4. 04

    Client cycles once

    disconnect and connect, at most every 30 seconds

  5. 05

    Handshake reads the token

    the current one, rooms are rejoined on connect

Figure 1. What happens now when a token goes stale in the background.

A genuine token change is not subject to the 30 second gap, because a new token is not a loop. It does reset the gap, so an auth_required arriving right after does not cycle a second time.

The same background commit also removed stale location tasks left by the legacy build. The operating system kept delivering fixes to a task that no longer had a handler, which logged an error on every fix and competed with the real task for the foreground service.

What I would do differently

I would have asked the server to tell the client about authentication state directly. The acknowledgement works, but it learns that the socket is anonymous one position at a time, and the 30 second gap is a defence against a loop that a clearer server signal would not need. The server middleware that reads the handshake token was already tracked as backend work, and until a clearer signal lands the client has to infer.

I would also have written the token listener first. The acknowledgement and the stored:false reconnect were the right defence, but they treat a symptom that is, in the end, a stale value copied once at connect time. Once the token store could say "I changed", the rest was small.

Finally, I would have tested the expired token path on a real device sooner. The scenario is a phone that sat in a pocket for a while, which is exactly the one a desk and a simulator never produce.

Where this fits

This is the middle chapter of the real-time work in Chiti: the first fix made the connection patient, this one made it honest about what the server actually did with the data, and the courier offers work that followed depended on both. The wider story, including the single typed connection and the cache bridge, is in the full Chiti case study.

The full project story explains the single socket these fixes sit on and the other incidents found on real devices.

Read the full case study

#socket-io#react-native#auth#reliability

Keep reading

Have a project in mind?