Real World Example

Live Rider Location

Swiggy, Zomato and Uber show the rider moving on a map. The rider app sends GPS updates every few seconds; the server forwards them to the customer watching that order, throttled so a slow phone is not flooded.

src/delivery/delivery.gateway.ts

@WebSocketGateway({ namespace: '/delivery' })
export class DeliveryGateway {
  @WebSocketServer() server: Server;
  private lastSent = new Map<string, number>();

  @SubscribeMessage('rider:location')
  location(@MessageBody() d: { orderId: string; lat: number; lng: number }, @ConnectedSocket() rider: Socket) {
    if (!rider.data.user?.roles.includes('rider')) return;
    const now = Date.now();
    if (now - (this.lastSent.get(d.orderId) ?? 0) < 3000) return;         // max one update / 3 s
    this.lastSent.set(d.orderId, now);
    this.server.to(`order:${d.orderId}`).volatile.emit('rider:location', { lat: d.lat, lng: d.lng, at: now });
  }
}

Note: volatile means "drop this message if the client is not ready". For location updates that is fine: the next one is a few seconds away.