maps · side quest · 2024
What's actually on the wire when a dot moves across a map
Every delivery app shows a marker gliding toward you. I wanted to know if that's polling or a held-open connection, so I built the smallest version I could: every open browser shares its live location, and every other browser sees it move — in about seventy lines across server and client.
The question
Polling, or a connection that stays open?
The moving dot in a delivery app is one of those things you stop noticing. I wanted to know what it costs: does the app ask “where is the driver now?” every few seconds, or does the server push positions down a connection that never closes — and how often is often enough?
Architecture
Every browser is both a driver and a viewer
To keep it honest and small, there’s no separate driver app: any browser that opens the page shares its own location and sees everyone else’s. An Express server serves the page and runs a Socket.io server alongside it on the same HTTP server.
One location update
From a GPS fix to a marker moving on someone else's screen
- 1The browser asks for continuous location with
watchPosition— high accuracy, no cached fixes, a five-second timeout. - 2Each new fix is emitted over the open socket as a
send-locationevent with latitude and longitude. - 3The server adds the sender’s socket id and broadcasts a
receive-locationevent to every connected client. - 4Each client keeps a marker per socket id: an existing marker moves with
setLatLng, a new id gets a new marker. - 5When a socket disconnects, the server broadcasts it and every map removes that marker.
io.on("connection", (socket) => {
socket.on("send-location", (data) => {
io.emit("receive-location", { id: socket.id, ...data });
});
socket.on("disconnect", () => {
io.emit("user-disconnected", socket.id);
});
});What I noticed
The interesting part is frequency, fan-out and privacy
- 1Updates are driven by the device, not a timer. The phone decides when it has a new fix. That’s efficient when you’re still and chatty when you’re moving — and too rare an update makes the dot teleport.
- 2Broadcast to everyone doesn’t scale. Every update goes to every client, so traffic grows with the square of the number of users. A real app sends a driver’s position only to the people waiting on that trip.
- 3Location is sensitive by default. Broadcasting raw coordinates to every connection is fine for a demo and unacceptable anywhere else.
Where I'd take it
From demo to something you could ship
- 1Rooms per trip, so a position only reaches the people who should see it.
- 2Throttle on the sender and interpolate on the viewer, so the marker glides instead of stepping.
- 3ETA, which needs routing on a road network rather than just a position.
Built with
- Node.js
- Express
- Socket.io
- Leaflet
- EJS