Overview
The Stream API service provides audio streaming functionality through a proxy layer that handles media URL resolution and stream delivery for Beat App. Base URL:${STREAM_API} (resolves to ${API_URL}/api/stream)
Configured in: src/constants.js
Configuration
src/constants.js
Base URL for API services. The Stream API is mounted at
/api/stream.Proxy Endpoint
Stream Proxy
The primary streaming endpoint that proxies audio requests through the backend server. Endpoint:GET ${STREAM_API}/proxy
Encoded media URL to proxy. Typically a YouTube Music stream URL.
Purpose & Architecture
Why Proxy Streaming?
The Stream API proxy serves several critical functions:- CORS Bypass: YouTube Music stream URLs have strict CORS policies that prevent direct browser playback from different origins. The proxy server adds appropriate CORS headers.
- URL Expiration: Direct stream URLs from YouTube Music expire quickly. The proxy can handle re-authentication and URL refresh transparently.
- Request Headers: Stream requests require specific headers and authentication that can’t be set from browser audio elements. The proxy manages these headers server-side.
- Rate Limiting: Centralizes stream requests through a single endpoint, allowing for rate limiting and monitoring.
Request Flow
Response Headers
The proxy endpoint should set appropriate headers for streaming:Media type of the audio stream (e.g.,
audio/mp4, audio/webm)Indicates server supports range requests (typically
bytes)Total size of the audio file in bytes
CORS header allowing browser access (e.g.,
* or specific origin)Caching directives for the stream
Error Handling
Common Error Scenarios
Client-Side Error Handling
Performance Considerations
Range Requests
The proxy should support HTTP range requests to enable:- Seeking within tracks
- Efficient bandwidth usage
- Progressive loading
Caching Strategy
While stream URLs expire, partial caching can improve performance:- Short-term cache: 5-10 minutes for active streams
- CDN edge caching: For popular tracks
- Client-side buffering: Browser handles automatically
Streaming Protocols
YouTube Music typically provides:- Adaptive formats: Multiple quality levels
- Codecs: AAC (audio/mp4), Opus (audio/webm)
- Bitrates: 128kbps to 256kbps
Security Considerations
URL Validation
The proxy server should validate that URLs:- Point to legitimate YouTube Music domains
- Don’t attempt to proxy arbitrary external URLs
- Include proper authentication tokens
Rate Limiting
Implement rate limiting to prevent abuse:- Per-IP limits: 100 requests per minute
- Per-user limits: 500 requests per hour
- Concurrent stream limits: 3 simultaneous streams
Integration with YouTube API
The Stream API works in conjunction with the YouTube API service:- Get track metadata via YouTube API (see YouTube API docs)
- Extract stream URL from track data
- Proxy stream through Stream API
- Play audio in browser
Environment Variables
Base URL for the API server hosting the stream proxy
Best Practices
- Always encode URLs: Use
encodeURIComponent()when constructing proxy URLs - Handle errors gracefully: Implement retry logic for network failures
- Preload next track: Start loading the next track 10-15 seconds before current ends
- Monitor stream health: Track buffering events and error rates
- Implement fallbacks: Have alternative quality streams available
Example: Complete Audio Player
Related Documentation
YouTube API
Get track metadata and stream URLs
API Overview
Backend architecture overview