A real-time team chat app: public channels, private messages, typing indicators, live presence, emoji reactions and reliable delivery across reconnects.
Live demo: https://relay-chatapp.vercel.app · API: https://relay-api-su3t.onrender.com/api/health · API: https://relay-api.onrender.com/api/health
Demo login: priya@relay.dev / Demo@2026 (open a second browser as rahul@relay.dev to watch messages arrive live).
The API runs on Render's free tier, so the first request after a quiet period can take ~30–50 s while the server wakes up.
- Channels and private chats. Create, join and leave public channels; start 1:1 private chats with anyone.
- Live updates over Socket.io. Messages, reactions, joins/leaves and new chats arrive instantly in every open tab.
- Typing indicators. "Priya is typing…" with throttled events and automatic expiry if a "stop" event is lost.
- Presence. Online dot and "last seen", correct across several tabs per user.
- Join/leave notices posted into the channel as system messages.
- Reliable sending. Optimistic messages (🕒 → ✓✓), acknowledgements with timeout, Retry on failure, no duplicates on resend.
- Reconnect handling. Connection banner; after reconnecting the app refreshes chats, reloads missed messages and resends anything pending.
- Unread counts per chat, emoji picker and quick reactions (👍 ❤️ 😂 😮 🙏 🔥).
- Real accounts with JWT + bcrypt, profile colour, and 5 themes (Aurora, Ocean, Sunset, Forest, Midnight).
- Responsive. Two-pane layout on desktop; list → chat navigation on mobile.
| Layer | Choice |
|---|---|
| Client | React 19, Vite, React Router 8 (createBrowserRouter), Context API + useReducer, socket.io-client, plain CSS with CSS variables |
| Server | Node.js, Express 5, Socket.io 4, Mongoose, Zod, JWT, bcrypt, helmet, rate limiting |
| Database | MongoDB Atlas |
| Tests | Jest + React Testing Library (client); Jest, Supertest, socket.io-client and mongodb-memory-server (server) |
| Hosting | Vercel (client) · Render (API + WebSocket server) · MongoDB Atlas |
Browser (React) Render (Node) Atlas
┌──────────────────────────────┐ REST ┌──────────────────────────┐ ┌─────────┐
│ Router: middleware + loaders │──────────▶ │ routes → controllers │──▶│ users │
│ ChatContext (useReducer) │ │ → models │ │ channels│
│ hooks: useSocketEvent, │ WebSocket │ socket/ → socketController│ │ messages│
│ useConnection, ... │◀─────────▶ │ presence (in memory) │ │ reads │
└──────────────────────────────┘ └──────────────────────────┘ └─────────┘
- Models (
server/src/models): User, Channel (public channel or DM), Message, Read (last-read time per user and chat). - Controllers (
server/src/controllers): the business rules, shared by HTTP and sockets.socketControllertreats each socket event like a request: validate with Zod → call a controller → reply through the acknowledgement as{ ok, data }or{ ok: false, error }. - Views: the JSON sent over REST and Socket.io (
toJSONPublic,toChannelJSON). - Rooms:
channel:<id>for every chat,user:<id>for all tabs of one user. On every (re)connect the server joins the socket to its rooms, then sendssession:ready.
client/src
├── router/ routes, middleware (session, auth guard), loaders
├── context/ AuthContext, ThemeContext, ChatContext + chatReducer + selectors
├── hooks/ useSocketEvent, useConnection, useTyping, useAutoScroll
├── lib/ api, socket factory, session store, formatting
├── pages/ route components (lazy loaded)
└── components/ ui, layout, chat, modals, auth
- Routing:
createBrowserRouterwith route middleware that restores the session and protects/chat, a loader that fetches chats and users before the page renders, lazy routes and an error element. - State: one
useReducerfor chat state. State and actions are provided by separate contexts so components that only send actions don't re-render on every message. - Socket listeners:
useSocketEventsubscribes once per event and always calls the latest handler, and removes the listener on unmount. This prevents duplicate listeners (and duplicate messages) under React StrictMode and avoids leaks.
- The composer creates a
clientIdand adds the message as sending (🕒). message:sendis emitted with a 10 s acknowledgement timeout.- The server checks membership and rate limit, then saves it. A unique index on
{ sender, clientId }makes a resend return the existing message instead of creating a duplicate. - The server broadcasts
message:newtochannel:<id>; the sender's ack turns 🕒 into ✓✓. - On timeout the message shows Retry. After a reconnect, pending messages are resent automatically.
Requirements: Node 22 (see .nvmrc) and a MongoDB connection string (Atlas free tier or local MongoDB).
# 1. API
cd server
cp .env.example .env # then set MONGO_URI and JWT_SECRET
npm install
npm run seed # demo users priya / rahul / amit @relay.dev
npm run dev # http://localhost:5000
# 2. Client (new terminal)
cd client
npm install
npm run dev # http://localhost:5173Open two different browsers (or one normal + one private window), log in as two demo users and chat.
cd client && npm test # reducer, hooks, auth form, router middleware
cd server && npm test # unit + REST + real Socket.io integration testsServer tests start an in-memory MongoDB, so the first run downloads a MongoDB binary.
- Server → Render (Web Service, root
server, buildnpm install, startnpm start). Environment:MONGO_URI,JWT_SECRET,CLIENT_ORIGIN(the Vercel URL),NODE_ENV=production. - Client → Vercel (root
client, framework Vite). Environment:VITE_API_URLandVITE_SOCKET_URL(both the Render URL).
The WebSocket server runs on Render rather than Vercel because it is a long-running process that holds connections open; serverless functions are not designed for that.
- Redis adapter for Socket.io so presence and rooms work across several server instances
- Message pagination ("load older"), editing and deleting
- Image attachments (Cloudinary) and read receipts per message
- Push notifications for offline users
MIT
