# qrusty_client

A Rust client for the qrusty priority queue server.

## Features

- HTTP API client (`QrustyClient`) — publish, consume, ack/nack, queue management, stats
- WebSocket client (`WsSession`) — push-based delivery with concurrent ack/nack/publish over a single connection
- Automatic retries with exponential back-off for transient HTTP errors
- Automatic Ping/Pong keep-alive and Close frame handling for idle WebSocket connections
- Async-first via Tokio

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
qrusty_client = "0.17"
```

## HTTP client

```rust
use qrusty_client::client::QrustyClient;

#[tokio::main]
async fn main() {
    let client = QrustyClient::new("http://localhost:6784");

    client.create_queue("orders", "MaxFirst", None, None).await.unwrap();
    let id = client.publish("orders", 100u64, r#"{"order_id":1}"#, None).await.unwrap();

    if let Some(msg) = client.consume("orders", "worker-1", None).await.unwrap() {
        client.ack("orders", &msg.id, "worker-1").await.unwrap();
    }
}
```

## WebSocket client

`WsSession` maintains a single connection and demultiplexes frames internally, so
`publish`, `ack`, `nack`, and `subscribe` can all be called concurrently without
deadlocking.

```rust
use qrusty_client::ws::{WsSession, DeliveredMessage};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let session = WsSession::connect("ws://localhost:6784").await?;

    // Publish a message.
    let id = session.publish("orders", r#"{"order_id":1}"#, Some(10u64.into())).await?;
    println!("published {}", id);

    // Subscribe and process messages, acking each one on the same session.
    let mut rx = session.subscribe("orders").await?;
    while let Some(Ok(msg)) = rx.recv().await {
        println!("received {} from {}", msg.payload, msg.queue);
        session.ack(&msg.queue, &msg.id).await?;
    }

    session.close().await?;
    Ok(())
}
```

## Development

```bash
cargo test
cargo publish  # requires a crates.io token: cargo login <your-token>
```

## Documentation

See [docs.rs/qrusty_client](https://docs.rs/qrusty_client) for full API documentation.

## License

MIT
