# RPC (/sdks/rust/reference/rpc)



## Command [#command]

Outbound command request. The sender blocks until a subscriber responds or the timeout expires.

| Field       | Type       | Default  | Description               |
| ----------- | ---------- | -------- | ------------------------- |
| `id`        | `String`   | UUID v4  | Request identifier        |
| `channel`   | `String`   | required | Target channel name       |
| `metadata`  | `String`   | `""`     | Optional metadata         |
| `body`      | `Vec<u8>`  | `[]`     | Command payload           |
| `timeout`   | `Duration` | 5s       | Maximum wait for response |
| `client_id` | `String`   | `""`     | Sender identity override  |
| `tags`      | `HashMap`  | `{}`     | Key-value pairs           |

### Builder [#builder]

```rust title="main.rs"
use std::time::Duration;

let cmd = Command::builder()
    .channel("device.reboot")
    .body(b"device-42".to_vec())
    .timeout(Duration::from_secs(10))
    .build();
```

## CommandResponse [#commandresponse]

Returned by `send_command`.

| Field                | Type      | Description                              |
| -------------------- | --------- | ---------------------------------------- |
| `command_id`         | `String`  | Original command request ID              |
| `response_client_id` | `String`  | Identity of the handler                  |
| `executed`           | `bool`    | Whether the command succeeded            |
| `executed_at`        | `i64`     | Server timestamp                         |
| `error`              | `String`  | Error message when `executed` is `false` |
| `tags`               | `HashMap` | Handler-attached tags                    |

## CommandReceive / CommandReply [#commandreceive--commandreply]

`CommandReceive` is delivered to the subscription callback. Use `CommandReply::builder()` to construct the response.

```rust title="main.rs"
let reply = CommandReply::builder()
    .request_id(&cmd.id)
    .response_to(&cmd.response_to)
    .build();
client.send_command_response(reply).await?;
```

## Query [#query]

Outbound query request with optional server-side caching.

| Field       | Type       | Default  | Description                        |
| ----------- | ---------- | -------- | ---------------------------------- |
| `id`        | `String`   | UUID v4  | Request identifier                 |
| `channel`   | `String`   | required | Target channel name                |
| `metadata`  | `String`   | `""`     | Optional metadata                  |
| `body`      | `Vec<u8>`  | `[]`     | Query payload                      |
| `timeout`   | `Duration` | 5s       | Maximum wait for response          |
| `cache_key` | `String`   | `""`     | Cache key (empty disables caching) |
| `cache_ttl` | `Duration` | 0        | Cache time-to-live                 |
| `tags`      | `HashMap`  | `{}`     | Key-value pairs                    |

### Builder [#builder-1]

```rust title="main.rs"
use std::time::Duration;

let query = Query::builder()
    .channel("inventory.lookup")
    .body(b"sku-12345".to_vec())
    .timeout(Duration::from_secs(10))
    .cache_key("sku-12345")
    .cache_ttl(Duration::from_secs(60))
    .build();
```

## QueryResponse [#queryresponse]

Returned by `send_query`.

| Field       | Type      | Description                              |
| ----------- | --------- | ---------------------------------------- |
| `query_id`  | `String`  | Original query request ID                |
| `executed`  | `bool`    | Whether the query succeeded              |
| `metadata`  | `String`  | Response metadata                        |
| `body`      | `Vec<u8>` | Response payload                         |
| `cache_hit` | `bool`    | Whether served from cache                |
| `error`     | `String`  | Error message when `executed` is `false` |

## Client Methods [#client-methods]

### send\_command / send\_query [#send_command--send_query]

```rust title="main.rs"
let resp = client.send_command(cmd).await?;
let resp = client.send_query(query).await?;
```

### subscribe\_to\_commands / subscribe\_to\_queries [#subscribe_to_commands--subscribe_to_queries]

```rust title="main.rs"
let sub = client.subscribe_to_commands("channel", "", handler, None).await?;
let sub = client.subscribe_to_queries("channel", "", handler, None).await?;
```

### send\_command\_response / send\_query\_response [#send_command_response--send_query_response]

```rust title="main.rs"
client.send_command_response(reply).await?;
client.send_query_response(reply).await?;
```
