#json-rpc #api-client #control #tokio #control-api #async-client #snapcast

snapcast-control

a wrapper for the Snapcast JSON-RPC Control API based on tokio

1 unstable release

0.1.0 Jun 25, 2024

#19 in #control-api

24 downloads per month

MIT license

100KB
2K SLoC

snapcast-control

snapcast-control is a Rust api client for Snapcast. It supports all features of the Snapcast JSON-RPC API as of version 0.28.0 (2024/6/25).

Documentation is available at docs.rs.

Features

  • native rust types for all api requests and responses
  • tokio-based async client
  • client with helper methods for all api requests
  • automatic socket reconnection via stubborn-io

Installation

cargo add snapcast-control

Usage

The best example of this crate's usage is snapcast-multiroom, the project I designed it for.

A simple example of usage:

use snapcast_control::{SnapcastConnection, ValidMessage};

#[tokio::main]
async fn main() {
  let mut client = SnapcastConnection::open("127.0.0.1:1705".parse().expect("could not parse socket address")).await;

  // client state is updated with each message received
  let state = client.state.clone();

  // state is empty initially, sending the server_get_status request will populate it
  client.server_get_status().await.expect("could not send request");

  loop {
    tokio::select! {
      // as messages are received, they are stored in the client's recv buffer
      Some(message) = client.recv() => {
        if let Ok(response) = message {
          // handle response
          match response {
            ValidMessage::Result { id, jsonrpc, result  } => {},
            ValidMessage::Notification { method, jsonrpc } => {},
          }
        } else if let Err(err) = message {
          // handle error
        }
      },
      _ = tokio::signal::ctrl_c() => {
        break;
      }
    }
  }
}

Dependencies

~6–16MB
~194K SLoC