A single Node process holds thousands of sockets, but production chat needs horizontal scale. The key is making any server able to deliver to any user.
Fan-out with Redis
Each server subscribes to channels for the users connected to it. When a message arrives, it is persisted first, then published; the owning server pushes it down the socket.
io.adapter(createAdapter(pub, sub));
socket.on("msg", async m => {
await db.messages.insert(m);
io.to(m.room).emit("msg", m);
});Persist first, publish second. Never the other way around.
Add delivery receipts and idempotency keys, and the client can safely retry on flaky mobile networks.