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



Commands and queries implement request–reply over channels. Commands expect a single response; queries return data payloads and support optional response caching.

## Commands [#commands]

### SendCommand [#sendcommand]

```go
func (c *Client) SendCommand(ctx context.Context, command *Command) (*CommandResponse, error)
```

Sends a command and blocks until a response arrives or timeout elapses.

**Parameters:**

| Name      | Type              | Required | Description                                |
| --------- | ----------------- | -------- | ------------------------------------------ |
| `ctx`     | `context.Context` | Yes      | Overall deadline                           |
| `command` | `*Command`        | Yes      | Channel, body/metadata, `SetTimeout`, tags |

**Returns:** `*CommandResponse` — execution metadata and payload from responder.

### SubscribeToCommands [#subscribetocommands]

```go
func (c *Client) SubscribeToCommands(ctx context.Context, channel, group string, opts ...SubscribeOption) (*Subscription, error)
```

Receives incoming commands for a service. Use handlers configured via `SubscribeOption`.

### SendCommandResponse [#sendcommandresponse]

```go
func (c *Client) SendCommandResponse(ctx context.Context, response *CommandReply) error
```

Replies to a command received in the subscription callback.

## Queries [#queries]

### SendQuery [#sendquery]

```go
func (c *Client) SendQuery(ctx context.Context, query *Query) (*QueryResponse, error)
```

Sends a query and waits for a typed response. Use `SetCacheKey` / `SetCacheTTL` on the `Query` for server-side cache hints.

### SubscribeToQueries [#subscribetoqueries]

```go
func (c *Client) SubscribeToQueries(ctx context.Context, channel, group string, opts ...SubscribeOption) (*Subscription, error)
```

Subscribes as a query worker; analogous to commands with different protobuf routing.

### SendQueryResponse [#sendqueryresponse]

```go
func (c *Client) SendQueryResponse(ctx context.Context, response *QueryReply) error
```

Returns data and metadata to the caller of `SendQuery`.

## Message helpers [#message-helpers]

| Type      | Factory        | Notes                               |
| --------- | -------------- | ----------------------------------- |
| `Command` | `NewCommand()` | Timeout required for server routing |
| `Query`   | `NewQuery()`   | Adds cache key/TTL fields           |

## Quick Usage [#quick-usage]

```go title="rpc.go"
cmd := kubemq.NewCommand()
cmd.SetChannel("svc.commands")
cmd.SetBody([]byte("ping"))
cmd.SetTimeout(5 * time.Second)

resp, err := client.SendCommand(ctx, cmd)
if err != nil {
    return err
}
_ = resp.Executed
```

## See Also [#see-also]

* [RPC Examples](/sdks/go/how-to/rpc/)
* [RPC pattern](/learn/rpc/getting-started)
* [Go SDK Getting Started](/sdks/go)
