Niji Radio is a web application that allows multiple users to synchronize and play music together. It utilizes Cloudflare Workers and its Durable Objects to achieve real-time synchronization.
- Synchronized music playback across multiple users
- Dynamic playlist management (add, delete, clear)
- Real-time synchronization using WebSockets
- Music playback control in the browser
- Display of remaining time and time until the next track
- Runtime: Cloudflare Workers
- State Management: Durable Objects
- Framework: Hono
- Development Tool: Wrangler
MusicSyncObject provides the following functionalities:
- Dynamic playlist management
- Adding new tracks
- Automatic removal of played tracks
- Complete clearing of the playlist
- Synchronization of playback state
- WebSocket connection management
- Automation of track switching
- Simple HTML/JavaScript implementation
- Synchronization via WebSocket connection
- Music playback using the browser's Audio API
- Real-time display of remaining time and time until the next track
- Description: Provides the main web interface
- Response: HTML
- Description: Sets the playlist
- Behavior: Replaces the existing playlist with a new one
- Request body:
[
{
"url": "URL of the music file",
"duration": Duration in milliseconds
}
]
- Description: Adds tracks to the playlist
- Behavior: Appends new tracks to the existing playlist
- Request body:
[
{
"url": "URL of the music file",
"duration": Duration in milliseconds
}
]
- Description: Completely clears the playlist
- Behavior: Removes all tracks and stops playback
- Request body: None
- Description: Retrieves the current playlist
- Response: Playlist information (JSON)
- Synchronization message
{
"type": "sync",
"elapsedTime": Elapsed time in milliseconds,
"trackUrl": "URL of the currently playing track",
"duration": Track length in milliseconds
}
- Track change message
{
"type": "changeTrack",
"trackUrl": "URL of the new track",
"duration": Track length in milliseconds
}
# Install dependencies
npm install
# Start the development server
npm run dev
# Deploy to Cloudflare Workers
npm run deploy
-
Adding new tracks
- Add new tracks with
POST /api/addToPlaylist
- Appends to the end of the existing playlist
- Starts playback immediately if the playlist is empty
- Add new tracks with
-
Setting the playlist
- Set a new playlist with
POST /api/setPlaylist
- Replaces the existing playlist with the new one
- Immediately starts playing the first track of the new playlist
- Set a new playlist with
-
Management of played tracks
- Automatically removes tracks from the playlist when they finish playing
- Automatically starts playing the next track
- Stops playback if the playlist becomes empty
-
Clearing the playlist
- Completely clears the playlist with
POST /api/clearPlaylist
- Also stops the currently playing track
- Notifies all clients to stop playback
- Completely clears the playlist with
- Client establishes a WebSocket connection
- Server sends the current playback state (track URL, elapsed time, track length)
- Client adjusts the playback position based on the received information
- When a track ends, it automatically switches to the next track and notifies all clients
- Displays the remaining time and time until the next track in real time
- Playlist persistence
- Playback state management
- WebSocket connection management
- Track switching scheduling