Lessons from building real-time apps with Socket.IO and Node.js
By Prince Bhakodiya· 2 min read
In short
Use sockets for live, throwaway state (locations, prices, trip status) and REST plus a database for anything that must be kept (payments, orders, history). Group users into rooms, expect connections to drop and re-sync from the API when they come back, keep messages small, use Redis to share state across servers, and stop streaming when nobody needs the data.
I have built real-time features into ride-hailing apps like Vayu and Gogogo, and a live crypto app in Speartrades. These are the rules I keep coming back to.
Split live state from durable state
A driver's position five seconds ago is worthless; a payment five seconds ago is not. Send live, disposable data over sockets and write anything you must keep through a normal API into the database. When the two get mixed up, a dropped connection loses real data.
Use rooms for everything
Put the rider and driver of one trip in the same room, or every user watching one coin in one room. Broadcasting to a room is simple, and it stops you sending updates to people who do not need them.
Assume the connection will drop
Phones switch networks, go into tunnels and get killed in the background. On reconnect, fetch the current state from the API before trusting the socket again. The user should never see a stale trip or price.
Keep messages small and frequent
Send only what changed. A location update needs coordinates and a timestamp, not the whole driver profile. Smaller payloads mean smoother apps on slow networks.
Share state when you scale
One Node.js server is easy. With several, a user on server A must still hear about events on server B. A Redis adapter or pub/sub layer solves this, and Redis is also a good home for hot data like latest prices.
Be kind to batteries
Stream only while it matters. A driver who is offline does not need to send location, and a user who has left the price screen does not need ticks.
Keep reading
Building Vayu: a ride-hailing app with Flutter, Node.js and Socket.IO
How I built Vayu, a ride-hailing platform with separate rider and driver apps, real-time matching, live GPS tracking, wallet payments and multi-city support.
Gogogo: dispatch, fares and driver onboarding for on-demand transport
A look at Gogogo's production ride-hailing ecosystem: user and driver apps, a dispatch engine, fare calculation with surge, driver KYC and an operations dashboard.