A comprehensive backend API for the SubStream Protocol, supporting wallet-based authentication, tier-based content access, real-time analytics, and multi-region storage replication.
- Multi-resolution transcoding: Automatic conversion to 360p, 720p, and 1080p
- HLS streaming: Segmented video for smooth adaptive bitrate streaming
- Adaptive quality: Automatic quality selection based on connection speed
- Background processing: Queue-based transcoding with Redis
- Storage flexibility: Support for S3 and IPFS storage
- Pay-per-second integration: Seamless integration with subscription system
- Wallet-based authentication using Sign In With Ethereum
- JWT token generation and validation
- Nonce-based security
- Multi-tier user support (Bronze, Silver, Gold)
- View-time event aggregation
- On-chain withdrawal event tracking
- Heatmap generation for content engagement
- Server-sent events for real-time updates
- Creator analytics dashboard
- IPFS content replication across multiple services
- Automatic failover between regions
- Health monitoring and service recovery
- Support for Pinata, Web3.Storage, and Infura
- Content filtering based on user subscription tier
- Censored previews for unauthorized content
- Database-level access control
- Upgrade suggestions and tier management
- RabbitMQ integration: Reliable message queuing for background tasks
- Event-driven architecture: Non-blocking processing of heavy operations
- Retry logic: Automatic retry with exponential backoff for failed operations
- Circuit breaker: Prevents cascading failures during high load
- Dead letter queue: Failed message handling for debugging
- Background worker: Separate process for handling emails, notifications, and leaderboard updates
- Node.js 20.11.0+
- npm or yarn
- FFmpeg (for video transcoding)
- Redis (for job queue)
- RabbitMQ (for asynchronous event processing)
- Clone the repository:
git clone https://github.com/lifewithbigdamz/SubStream-Protocol-Backend.git
cd SubStream-Protocol-Backend- Install FFmpeg:
# Ubuntu/Debian
sudo apt-get update && sudo apt-get install -y ffmpeg
# macOS
brew install ffmpeg
# Windows
# Download from https://ffmpeg.org/download.html- Install and start Redis:
# Ubuntu/Debian
sudo apt-get install redis-server
sudo systemctl start redis
# macOS
brew install redis
brew services start redis
# Windows
# Download from https://redis.io/download- Install and start RabbitMQ:
# Ubuntu/Debian
sudo apt-get install rabbitmq-server
sudo systemctl start rabbitmq-server
# macOS
brew install rabbitmq
brew services start rabbitmq
# Windows
# Download from https://www.rabbitmq.com/download.html- Install dependencies:
npm install- Copy environment variables:
cp .env.example .env- Configure your environment variables in
.env:
- Set your JWT secret
- Add IPFS service API keys
- Configure Redis connection
- Configure RabbitMQ connection
- Set up S3 credentials (optional)
- Configure FFmpeg path
- Set up CDN base URL
- Start the services:
Option 1: Start API and Worker Together
npm run devOption 2: Start Services Separately (Recommended for Production)
# Terminal 1: Start the API server
npm run dev
# Terminal 2: Start the background worker
npm run worker:devThe API will be available at http://localhost:3000
The background worker will process events from RabbitMQ queues
GET /auth/nonce?address={address}- Get nonce for SIWEPOST /auth/login- Authenticate with wallet signature
GET /content- List content (filtered by user tier)GET /content/{id}- Get specific contentPOST /content- Create new content (requires authentication)PUT /content/{id}- Update content (creator only)DELETE /content/{id}- Delete content (creator only)GET /content/{id}/access- Check access permissionsGET /content/upgrade/suggestions- Get upgrade suggestions
POST /analytics/view-event- Record view-time eventPOST /analytics/withdrawal-event- Record withdrawal eventGET /analytics/heatmap/{videoId}- Get content heatmapGET /analytics/creator/{address}- Get creator analyticsGET /analytics/stream/{videoId}- Real-time analytics stream
POST /storage/pin- Pin content to multiple regionsGET /storage/content/{id}- Get content with failoverGET /storage/metadata/{id}- Get content metadataGET /storage/health- Check storage service healthGET /storage/url/{id}- Get content URLs
GET /- API informationGET /health- Health check
// 1. Get nonce
const nonceResponse = await fetch('/auth/nonce?address=0x742d35Cc6634C0532925a3b8D4C9db96C4b4Db45');
const { nonce } = await nonceResponse.json();
// 2. Sign message with wallet
const message = `Sign in to SubStream Protocol at ${new Date().toISOString()}\n\nNonce: ${nonce}\nAddress: 0x742d35Cc6634C0532925a3b8D4C9db96C4b4Db45`;
const signature = await signer.signMessage(message);
// 3. Login
const loginResponse = await fetch('/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ address, signature, message, nonce })
});
const { token } = await loginResponse.json();// Get content list (automatically filtered by tier)
const response = await fetch('/content', {
headers: { 'Authorization': `Bearer ${token}` }
});
const { content } = await response.json();
// Content will be full or censored based on user tier// Record view event
await fetch('/analytics/view-event', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
videoId: 'video_001',
watchTime: 120,
totalDuration: 300
})
});
// Get heatmap
const heatmapResponse = await fetch('/analytics/heatmap/video_001', {
headers: { 'Authorization': `Bearer ${token}` }
});- AuthService: Handles SIWE authentication and JWT management
- ContentService: Manages content with tier-based filtering
- AnalyticsService: Processes real-time analytics and generates heatmaps
- StorageService: Manages multi-region IPFS replication
- Authentication: JWT token validation
- Tier Access: Role-based access control
- Error Handling: Centralized error management
- User authenticates via wallet signature
- JWT token issued with tier information
- All subsequent requests include token
- Content filtered based on user tier
- Analytics events tracked in real-time
- Content replicated across multiple regions
See .env.example for all available configuration options.
npm testβββ routes/ # API route handlers
βββ middleware/ # Express middleware
βββ services/ # Business logic services
βββ docs/ # API documentation
βββ tests/ # Test files
βββ index.js # Main application entry
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
MIT License - see LICENSE file for details.
For issues and questions, please open an issue on GitHub.