A simple Rust WebSocket proxy server using Axum to know what track the specified user is currently listening to on Spotify. Server caching is used to keep the latest song in memory to not overload Spotify’s REST API.
A watch channel from tokio is being used to store the response of spotify and relay to newly connected WebSocket clients.
Background
I wanted to show my currently listening song from Spotify on my portfolio and wanted to have a proxy server so that I wouldn’t expose my Spotify credentials and get rate limited from Spotify’s API. I decided to make this code public so that I can more easily access the binary on any server where I would deploy this.
Performance
I have this running on my server (1GB RAM + 1 CPU core) and it is not taking a lot of resources - in part since it has been written in Rust. It is using on average 1% of the memory (~10MB) and 2% of the CPU. In addition, the resulting binary (that is being built in GitHub Actions) is less than 1MB.
Crates
Although I can probably make it even more performant by using fewer crates (Rust libraries/packages), it is way easier to get something up and running with them.
- * clap is a very useful crate transforming a Rust struct to CLI parameters, which also creates the
--help
command. - * axum helps with the handling of WebSockets
- * serde deserializes JSON to Rust structs
- * reqwest a simple HTTP client, used to call the Spotify API which also has good integration with
serde
🧑💻 Related Projects
🗞 Source Code
- Available on
- * GitHub (BenJeau/currently_playing_spotify)