This demo showcases a real-time leaderboard application using DiceDB. Writing realtime applications with DiceDB is as simple as writing a SQL query.
The application consists of two main components:
- A Go backend that interacts with DiceDB and serves WebSocket connections
- A simple HTML frontend that displays the leaderboard in real-time
The backend randomly generates player scores and updates them in DiceDB using a background thread.
It then uses QWATCH
to monitor changes and broadcasts updates to connected WebSocket clients.
docker-compose up
- Setup and run DiceDB (refer)
- Execute the following commands
$ cd examples/leaderboard.go
$ go run main.go
Open a web browser and navigate to http://localhost:8000
to view the leaderboard
main.go
: The Go backend applicationindex.html
: The frontend HTML fileDockerfile
: Defines the Docker image for the applicationdocker-compose.yaml
: Compose setup which launches a DiceDB instance and the Go backend
- The Go backend connects to DiceDB and starts updating random player scores every few milliseconds
- A
QWATCH
query is set up to monitor changes in player scores above a threshold - When the query's key-set receives updates, the backend is notified of the updated results
- The backend then broadcasts the updated leaderboard to all connected WebSocket clients
- The frontend establishes a WebSocket connection and updates the leaderboard in real-time as it receives data