A high-performance, scalable backend for the Relay real-time chat application, built with modern technologies to ensure reliability, speed, and type safety.
Frontend Repository: https://github.com/SuperSahitya/relay
- Runtime: Node.js
- Language: TypeScript
- Framework: Express.js
- Database: PostgreSQL (via Drizzle ORM)
- Real-time: Socket.io (with Redis Pub/Sub)
- Message Queue: Apache Kafka
- Caching & Store: Redis
- Authentication: Better Auth
- Validation: Zod
- Logging: Pino
- Real-time Messaging: Instant, low-latency chat powered by websockets (Socket.io).
- Event-Driven Architecture: Utilizes Apache Kafka to decouple message ingestion from persistence, ensuring high throughput and reliability.
- Scalable WebSocket Infrastructure: Utilizes the Redis Pub/Sub model with websockets (Socket.io) to support horizontal scaling across multiple server instances.
- Robust Presence System: Real-time user online/offline tracking using Redis keys with TTL-based heartbeats, with Pub/Sub used for presence update fanout.
- Secure Authentication: Powered by Better Auth, implementing:
- JWT & HTTP-Only Cookies: Secure session management using JSON Web Tokens stored in HTTP-only cookies to prevent XSS attacks.
- Google OAuth: Seamless social login integration.
- Email & Password: Secure credential-based login with mandatory email verification, and the credentials securely hashed and salted before storage.
- SMTP Integration: Uses Nodemailer to send verification emails via an SMTP server.
- Secure & Rate Limited: Protected by robust authentication middleware and Redis-backed rate limiting to prevent abuse.
- Type-Safe Development: Comprehensive TypeScript integration with Zod for runtime request validation.
POST /api/auth/*- Handled by Better Auth (Sign up, Sign in, etc.)
GET /api/users/me- Get current user profileGET /api/users/:id- Get specific user profileGET /api/search- Search for users
GET /api/friends- Get list of friendsPOST /api/friends- Send a friend requestPUT /api/friends/respond- Accept or decline a friend requestDELETE /api/friends/:friendId- Remove a friendGET /api/friends/requests- Get received friend requestsGET /api/friends/sent- Get sent friend requests
GET /api/messages/:friendId- Get chat history with a friend
chat_message: Send a message to a user.- Payload:
{ receiverId: string, message: string }
- Payload:
chat_message: Receive a new message.presence_update: Receive updates on friends' online status.
-
Clone the repository
git clone https://github.com/SuperSahitya/relay-server.git cd relay-server -
Install dependencies
pnpm install
-
Start Infrastructure (Docker)
This project uses Docker Compose to run PostgreSQL, Redis, and Apache Kafka.
docker-compose up -d
-
Environment Variables
Create a
.envfile and configure the environment variables as per.env.example. -
Run Database Migrations
pnpm db:push
-
Start the Server
pnpm dev