Note: This project was built as part of the Railway Senior Full-Stack Engineer - Support application. The assignment: "Build a Discord bot that streams messages from a Discord channel into a web front-end, with a backend component."
A robust Socket.IO backend for real-time Discord message streaming with authentication, rate limiting, and comprehensive error handling.
- 🔐 JWT Authentication - Secure user authentication with token-based sessions
- 📡 Socket.IO Integration - Real-time bidirectional communication
- 🤖 Discord Bot Integration - Seamless Discord API integration
- 🏥 Health Monitoring - Comprehensive health checks and metrics
- 🔒 Security - Helmet.js security headers and CORS protection
- 📊 Metrics - Real-time connection and performance metrics
- 🛡️ Error Handling - Comprehensive error handling and logging
- Runtime: Node.js with TypeScript
- Web Framework: Express.js
- Real-time: Socket.IO
- Discord: discord.js library
- Security: Helmet.js, CORS, JWT
- Monitoring: Custom health checks and metrics
- Node.js 18+
- MySQL 8.0+
- Redis 6.0+
- Discord Bot Token
-
Clone and install dependencies:
npm install
-
Set up environment variables:
cp env.example .env # Edit .env with your configuration
-
Start the server:
# Development npm run dev # Production npm run build npm start
GET /api/health
- Server health statusGET /api/metrics
- Real-time metricsGET /api/status
- Detailed server statusGET /api/ready
- Load balancer readinessGET /api/live
- Liveness probe
get_channels
- Fetch user's available channelsjoin_channel
- Join a Discord channelleave_channel
- Leave a Discord channel
channels_list
- Available channelsuser_info
- User informationmessage
- New message receivedmessage_update
- Message editedmessage_delete
- Message deletedmessages_bulk
- Message historychannel_update
- Channel informationerror
- Error notificationrate_limited
- Rate limit exceeded
{
"id": "123456789",
"content": "Hello, world!",
"author": {
"id": "987654321",
"username": "Alice"
},
"timestamp": "2024-01-01T12:00:00.000Z",
"channelId": "123456789",
"serverId": "111222333"
}
{
"channelId": "123456789"
}
[
{
"id": "123456789",
"name": "general",
"type": "text",
"serverId": "111222333"
}
]
This repository contains only the backend service. The frontend should connect to the backend via Socket.IO, authenticate with a JWT, and display messages in real time. If you need a reference implementation, see https://discord-bot.vercel.app (replace with your actual repo if available).
# Development
npm run dev # Start with nodemon
npm run build # Build TypeScript
npm start # Start production server
npm test # Run tests
npm run lint # Lint code
npm run lint:fix # Fix linting issues
backend/
├── src/
│ ├── handlers/ # Socket.IO event handlers
│ ├── middleware/ # Authentication & error handling
│ ├── namespaces/ # Socket.IO namespace setup
│ ├── routes/ # HTTP API routes
│ ├── services/ # Business logic services
│ ├── utils/ # Database, rate limiting utilities
│ ├── types.ts # TypeScript type definitions
│ └── server.ts # Main server file
├── tests/ # Test files
├── package.json
├── tsconfig.json
├── env.example
└── README.md
{
"status": "healthy",
"timestamp": "2024-01-01T12:00:00.000Z",
"uptime": 3600,
"connections": {
"total": 150,
"discord": 45
},
"memory": {
"used": "45MB",
"total": "128MB"
},
"environment": "production"
}
{
"connections": {
"total": 150,
"discord": 45
},
"rooms": {
"total": 25,
"channels": 15,
"activeChannels": [
{
"channelId": "123456789",
"userCount": 5
}
]
},
"performance": {
"eventLoopDelay": 123456789n,
"cpuUsage": {
"user": 123456,
"system": 789012
}
}
}
This project includes unit tests for authentication, message handling, and error handling. To run tests:
npm test
- Socket.IO is used for real-time streaming and channel-based rooms, allowing efficient message delivery to only those users who have joined a channel.
- JWT provides secure, stateless authentication for all socket connections.
- Modular code structure makes it easy to extend the backend with new features, events, or integrations.
- Health and metrics endpoints are included for easy deployment monitoring and observability.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3001
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3001/api/health || exit 1
CMD ["npm", "start"]
version: '3.8'
services:
discord-backend:
build: .
ports:
- "3001:3001"
environment:
- NODE_ENV=production
- DATABASE_URL=mysql://postgres:password@db:5432/discord_stream
- REDIS_URL=redis://redis:6379
depends_on:
- db
- redis
restart: unless-stopped
db:
image: mysql:8.0
environment:
- MYSQL_DATABASE=discord_stream
- MYSQL_ROOT_PASSWORD=password
volumes:
- mysql_data:/var/lib/mysql
restart: unless-stopped
redis:
image: redis:7-alpine
volumes:
- redis_data:/data
restart: unless-stopped
volumes:
mysql_data:
redis_data:
- JWT Authentication - Secure token-based authentication
- CORS Protection - Configurable CORS policies
- Helmet.js - Security headers and CSP
- Input Validation - Message content validation
- Error Handling - Comprehensive error logging
- Socket.IO Optimization - Configurable ping/pong intervals
- Memory Management - Proper cleanup on disconnect
- Graceful Shutdown - Clean resource cleanup
-
Discord Bot Not Connecting
- Verify bot token is correct
- Check bot has required permissions
- Ensure bot is added to servers
-
Socket.IO Connection Issues
- Verify CORS configuration
- Check authentication token
- Ensure frontend URL is correct
The server provides detailed logging:
🔐
- Authentication events🔗
- Connection events📨
- Message events👥
- Channel join/leave events📊
- Statistics and metrics❌
- Error events✅
- Success events
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License - see LICENSE file for details.