Files
dashboard-backend/README.md

195 lines
3.3 KiB
Markdown

# Dashboard Backend
Real-time cryptocurrency price streaming server built with Express.js and Socket.IO
## 🎯 Features
- ✅ Real-time cryptocurrency prices via WebSocket
- ✅ CoinGecko API integration (free, no API key needed)
- ✅ Automatic data refresh every 5 seconds
- ✅ CORS enabled for cross-origin requests
- ✅ Connection tracking and client count broadcasting
- ✅ Graceful error handling
- ✅ Automatic reconnection support
## 🛠 Tech Stack
- **Framework**: Express.js v4.18+
- **Real-time**: Socket.IO v4.5+
- **HTTP Client**: Axios v1.5+
- **Environment**: Dotenv v16+
- **CORS**: CORS middleware v2.8+
## 📋 Prerequisites
- Node.js v16.0.0 or higher
- npm v8.0.0 or higher
## 🚀 Quick Start
### 1. Installation
```bash
npm install
```
### 2. Environment Setup
Create a `.env` file:
```env
PORT=5000
NODE_ENV=development
CORS_ORIGIN=http://localhost:3000
```
### 3. Start Development Server
```bash
npm run dev
```
Server will run on `http://localhost:5000`
### 4. Start Production Server
```bash
npm start
```
## 📡 WebSocket Events
### Client → Server
**requestData**
- Payload: `{ dataType: 'crypto' }`
- Description: Client requests crypto data
### Server → Client
**data**
- Payload: Real-time cryptocurrency data
- Emitted: Every 5 seconds
**clientCount**
- Payload: `{ count: number }`
- Description: Total connected clients
**error**
- Payload: `{ message: string, details: string }`
- Description: Error occurred
## 📊 Data Response Format
```json
{
"type": "crypto",
"timestamp": "2025-10-28T12:00:00.000Z",
"prices": {
"bitcoin": {
"usd": 42500.50,
"usd_market_cap": 850000000000,
"usd_24h_vol": 25000000000,
"usd_24h_change": 2.5
},
"ethereum": {
"usd": 2300.75,
"usd_market_cap": 280000000000,
"usd_24h_vol": 15000000000,
"usd_24h_change": 1.8
}
}
}
```
## 🔄 Supported Cryptocurrencies
- Bitcoin (BTC)
- Ethereum (ETH)
- Cardano (ADA)
- Solana (SOL)
- Ripple (XRP)
## 🔒 Security
- Environment variables for sensitive data
- CORS restrictions configurable
- Input validation on events
- Error messages sanitized
- No sensitive data exposed
## 🐛 Debugging
### Enable Verbose Logging
The server logs all important events:
- Client connections/disconnections
- Data requests
- API calls
- Errors
Check console output for troubleshooting
### Common Issues
**Port Already in Use**
```bash
# Mac/Linux
lsof -i :5000
# Windows
netstat -ano | findstr :5000
```
**CORS Errors**
- Check CORS_ORIGIN in .env matches frontend URL
- Verify frontend is running on http://localhost:3000
**API Not Responding**
- Check internet connection
- CoinGecko API may be rate-limited
- Server will auto-retry
## 📈 Performance
- Lightweight and fast
- Handles multiple concurrent connections
- Efficient data updates (5-second intervals)
- Minimal bandwidth usage
## 🚀 Deployment Ready
Tested and ready for deployment on:
- Railway.app ✅
- Render.com ✅
- Heroku ✅
- DigitalOcean ✅
- AWS ✅
## 📝 API Health Check
```bash
curl http://localhost:5000/health
```
Response:
```json
{
"status": "OK",
"message": "Server is running",
"timestamp": "2025-10-28T12:00:00.000Z"
}
```
## 📄 License
ISC
## 👤 Author
CloudForge Dev Team
---
**Status**: Production Ready
**Last Updated**: 2025-10-28