A real-time chat application built with Next.js and Redis Pub/Sub.
- Real-time messaging with Server-Sent Events (SSE)
- Self-destructing chat rooms with automatic expiration
- Optimized Redis operations with connection pooling and pipelining
- Room management with owner privileges
- Node.js 18+ or Bun
- Redis server (local or remote)
- Clone the repository and install dependencies:
bun install
# or
npm install- Configure your environment variables:
Copy .env.example to .env and update with your Redis URL:
cp .env.example .envUpdate the .env file:
REDIS_URL=redis://localhost:6379For Redis with authentication:
REDIS_URL=redis://:your-password@localhost:6379- Start the development server:
bun dev
# or
npm run devThe application implements several Redis optimizations:
- Connection Pooling: Singleton pattern ensures efficient connection reuse
- Pipelining: Batch operations are executed using Redis pipelines
- Pub/Sub: Real-time updates use Redis Pub/Sub with Server-Sent Events
- Automatic Serialization: JSON data is automatically serialized/deserialized
- src/lib/redis.ts: Optimized Redis wrapper with connection pooling
- src/lib/realtime.ts: Custom real-time implementation using Redis Pub/Sub
- src/lib/realtime-client.ts: Client-side SSE consumer
- src/app/api/realtime/route.ts: SSE endpoint for real-time updates
POST /api/room/create- Create a new chat roomPOST /api/room/join- Join an existing roomGET /api/room/sudo- Check owner privilegesGET /api/room/ttl- Get room expiration timeDELETE /api/room- Destroy a room (owner only)POST /api/messages- Send a messageGET /api/messages- Get all messages in a roomDELETE /api/messages- Delete a message (owner only)GET /api/realtime- SSE endpoint for real-time updates
- Batch Operations: Multiple Redis commands are executed in a single pipeline
- Connection Reuse: Single Redis connection is reused across all requests
- Optimized JSON Handling: Automatic serialization/deserialization
- Keep-Alive: SSE connections use keep-alive to prevent timeouts
- Error Recovery: Automatic retry logic for failed Redis operations
| Variable | Description | Default |
|---|---|---|
REDIS_URL |
Redis connection URL | redis://localhost:6379 |
For production, consider:
- Use a managed Redis service (AWS ElastiCache, Redis Cloud, etc.)
- Enable Redis persistence (RDB or AOF)
- Set up Redis Sentinel or Cluster for high availability
- Configure proper Redis memory policies
- Enable TLS for Redis connections
Example production Redis URL:
REDIS_URL=rediss://:password@your-redis-host:6380MIT