✅ Backend complete. This project originally ran on Supabase (Auth, Postgres, Realtime). Supabase has been fully replaced by a hand-rolled backend — an Express 5 REST API, PostgreSQL, and a raw WebSocket layer. Auth, persistence, and live updates all work end to end.
Group Chat is a small full-stack app. The frontend is built with the Next.js App Router (Server Components + Server Actions); the backend is written from scratch: an Express API, Postgres for persistence, JWT auth in an httpOnly cookie, and a ws WebSocket server for realtime — everything Supabase used to provide, rebuilt by hand.
It's a learning project, and rebuilding the backend without a BaaS was the point.
- 💬 Channels — create, browse, and delete channels from the home page
- ⚡ Live messages — new messages are pushed over a WebSocket to everyone in the channel, no refresh needed
- 🔐 Real auth — email + password sign-up, bcrypt-hashed passwords, 7-day JWT in an httpOnly cookie
- 🧑 Message bubbles — sender name, avatar initials, and timestamps, with your own messages aligned to the right
- 🖥️ Server-rendered — channel lists and message history load in async React Server Components
- 🎨 Styled with CSS Modules — scoped styles per route, Geist font via
next/font
| Layer | Choice |
|---|---|
| Frontend | Next.js 16 (App Router), React 19, TypeScript |
| API | Express 5 on Node.js (ESM) |
| Database | PostgreSQL via pg |
| Realtime | ws — raw WebSockets on the same server |
| Auth | jsonwebtoken + bcrypt, JWT in an httpOnly cookie |
| Styling | CSS Modules + next/font (Geist) |
- Reads —
page.tsxandchannels/[id]/page.tsxare async Server Components that fetch/me,/channels, and/channels/:id/messagesfrom the API withcache: 'no-store', forwarding the JWT cookie. A missing or rejected token redirects to/login. - Writes — forms post to Server Actions in
actions.ts(createChannel,sendMessage,deleteChannel), which call the API andrevalidatePath. - Realtime —
messages.tsxopens a WebSocket to the API and sends{ type: "subscribe", channelId }. When anyone posts a message, the server broadcasts amessage_createdevent to every socket subscribed to that channel, and the client merges it into the list (deduped by id). - Auth —
/registerhashes the password with bcrypt (username is derived from the email prefix);/loginverifies it and sets the JWT cookie; arequireAuthmiddleware guards every data route; the WebSocket handshake verifies the same cookie before accepting the connection.
All data routes require the JWT cookie (or Authorization: Bearer <token>).
| Method | Route | Auth | Description |
|---|---|---|---|
| POST | /register |
– | Create an account |
| POST | /login |
– | Verify credentials, set the JWT cookie |
| POST | /logout |
– | Clear the cookie |
| GET | /me |
🔒 | Current user from the token |
| GET | /channels |
🔒 | List channels |
| POST | /channels |
🔒 | Create a channel (names are unique) |
| DELETE | /channels/:id |
🔒 | Delete a channel |
| GET | /channels/:id/messages |
🔒 | Message history with sender usernames |
| POST | /channels/:id/messages |
🔒 | Send a message (max 2000 chars) + broadcast |
| GET | /health |
– | DB connectivity check |
WebSocket — connect to the same origin (ws:// in dev, wss:// in prod), then send { "type": "subscribe", "channelId": "…" } to receive message_created events for that channel.
server/ # Express backend
├── index.js # REST API, auth middleware, WebSocket server
├── db.js # pg connection pool (DATABASE_URL)
└── schema.sql # users, channels, messages — uuid PKs, cascade deletes
src/app/ # Next.js frontend
├── page.tsx # Home: channel list (redirects to /login when signed out)
├── layout.tsx # Root layout, fonts, metadata
├── actions.ts # Server Actions: createChannel, sendMessage, deleteChannel, signOut
├── login/page.tsx # Email/password log in + sign up
└── channels/[id]/
├── page.tsx # Channel view: message history + composer
└── messages.tsx # Renders messages and subscribes to the WebSocket
- Node.js 20+
- A PostgreSQL database (local, or hosted like Neon)
git clone <your-repo-url>
cd groupchat
npm install
cd server && npm installpsql "$DATABASE_URL" -f server/schema.sqlserver/.env:
DATABASE_URL=postgres://user:pass@host/db
JWT_SECRET=any-long-random-string
# optional:
PORT=4000
FRONTEND_ORIGIN=http://localhost:3000.env.local (repo root):
NEXT_PUBLIC_API_URL=http://localhost:4000cd server && npm run dev # API + WebSocket on :4000
npm run dev # Next.js on :3000 (separate terminal)Open http://localhost:3000, sign up, create a channel — then open the same channel in a second browser to watch messages arrive live.
Frontend (repo root):
| Command | Description |
|---|---|
npm run dev |
Start the Next.js dev server |
npm run build |
Create a production build |
npm run start |
Run the production build |
npm run lint |
Lint with ESLint |
Server (server/):
| Command | Description |
|---|---|
npm run dev |
Start the API with node --watch |
npm run start |
Start the API |
Three pieces, deployed separately:
| Piece | Where | Env |
|---|---|---|
| Frontend | Vercel — grpchat.org | NEXT_PUBLIC_API_URL=https://api.grpchat.org |
| API + WebSocket | Render — api.grpchat.org |
DATABASE_URL, JWT_SECRET, FRONTEND_ORIGIN, COOKIE_DOMAIN, NODE_ENV=production |
| Postgres | Neon | Apply server/schema.sql once |
Serving the API from a subdomain keeps auth same-site: the cookie is set for the shared domain (COOKIE_DOMAIN, e.g. .grpchat.org), so the browser sends it to both the site and the API — including on the WebSocket handshake.
