In my previous post I said I'm building a motorcycle app. The feature people ask about most is the live map: open it, see riders who are on the road right now, moving in real time.

With ten users it's easy. With real users it gets interesting. Here is how it works in BRAP (Expo + Supabase + Mapbox), and what production taught me.

The idea: one row per ride

A ride in progress is a single row with a user id, lat, lng and a timestamp. Row exists = riding. Ride ends = row deleted. Everything else follows:

  • who is riding? a select
  • someone moved? an UPDATE event
  • someone stopped? a DELETE event

The phone writes its position from a background task (expo-location). Two details that cost me days:

  • iOS only honours distanceInterval. timeInterval is ignored by the native code. A tick every 25 m means the update rate follows the speed, which is what you want anyway.
  • Android needs foregroundService. Without that option there is no background tracking at all, and the system cuts location to a few updates per hour.

The client: events patch the cache

I don't keep riders in useState. The first snapshot is a normal React Query useQuery, and every realtime event patches that cache:

queryClient.setQueryData<Rider[]>(queryKey, (prev) => (prev ?? []).map((r) => r.id === loc.user_id ? { ...r, lat: loc.lat, lng: loc.lng } : r, ), ) 

One source of truth, and a refetch on resume simply works. One catch: the event payload has no username or avatar, so for a new rider I refetch the snapshot instead of patching.

Markers are real React views (MarkerView from @rnmapbox/maps) with avatars. Mapbox's built-in clustering only works with GeoJSON layers, so I group overlapping markers myself.

A small trap: give every channel a random suffix. removeChannel is async and React doesn't wait for it, so supabase.channel('same-name') can return the old, already subscribed instance and .on() throws "cannot add callbacks after subscribe()".

Three lessons that cost real CPU

1. Subscribe only while someone is looking.
In Expo Router a tab is not unmounted when you leave it. My subscription lived until the app was closed, so people browsing the feed still received every rider's every move. Now it is active only when the screen is focused and the app is in the foreground. Both are needed: focus knows nothing about minimising, AppState knows nothing about tabs. I release it with a 30 second delay so quick tab switching doesn't resubscribe constantly.

2. Postgres Changes scales with subscribers, not just writes.
Every change is checked against your row-level security policy separately for each subscriber. Position updates are the most frequent writes in a tracking app, so this quickly became the single biggest CPU item in my database, bigger than all my own queries together. Make the policy cheap (no extra lookups on the hot path), and look at your stats before launch, not after.

3. Realtime is a hint, not a log.
If the socket sleeps (background, tunnel, no network), the events that happened meanwhile are never replayed. Result: a rider finished, but their marker hung on my map forever. The fix is boring: refetch on resume and on reconnect. On React Native you must wire both bridges by hand, otherwise React Query's refetchOnWindowFocus and refetchOnReconnect silently do nothing:

AppState.addEventListener('change', (s) => focusManager.setFocused(s === 'active')) onlineManager.setEventListener((setOnline) => NetInfo.addEventListener((s) => setOnline(!!s.isConnected && s.isInternetReachable !== false), ), ) 

Bonus: I also drop on the client any ride whose last update is very old. One user revoked location permission and stayed on the map as "riding" for 24 hours.

If I started again

I would look at Broadcast for the high-frequency positions and keep Postgres Changes for rare events (joined, left, finished). I haven't migrated yet, so I can't give you numbers, only the direction.

Takeaways

  1. One row per ride keeps client and policies simple.
  2. Subscribe only while the screen is focused and the app is foreground.
  3. Realtime never catches up: always have a refetch path.
  4. Postgres Changes cost grows with subscribers. Keep the policy cheap.
  5. On mobile, wire up focusManager and onlineManager yourself.

You can see it working on a real road in BRAP: brap-app.com. If you solved live location on Supabase another way (Broadcast? Presence? your own socket?), tell me in the comments.

Peace!