sarthak bhardwaj.
0%
Skip to content
← work

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.

Real-time location sharing over websocketsBrowser AwatchPosition → emitBrowser BwatchPosition → emitSocket serverExpress + Socket.ioEvery client's mapLeaflet · marker per idOn disconnectmarker removedsend-locationsend-locationbroadcast to alluser-disconnected
Positions flow up from each browser and are broadcast back down to all of them — no polling anywhere.

One location update

From a GPS fix to a marker moving on someone else's screen

  1. 1
    The browser asks for continuous location with watchPosition — high accuracy, no cached fixes, a five-second timeout.
  2. 2
    Each new fix is emitted over the open socket as a send-location event with latitude and longitude.
  3. 3
    The server adds the sender’s socket id and broadcasts a receive-location event to every connected client.
  4. 4
    Each client keeps a marker per socket id: an existing marker moves with setLatLng, a new id gets a new marker.
  5. 5
    When a socket disconnects, the server broadcasts it and every map removes that marker.
app.js — the entire server-side protocol
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

  1. 1
    Updates 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.
  2. 2
    Broadcast 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.
  3. 3
    Location 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

  1. 1
    Rooms per trip, so a position only reaches the people who should see it.
  2. 2
    Throttle on the sender and interpolate on the viewer, so the marker glides instead of stepping.
  3. 3
    ETA, which needs routing on a road network rather than just a position.

Built with

  • Node.js
  • Express
  • Socket.io
  • Leaflet
  • EJS