# Java Examples

> Runnable per-modality examples in the SDK split repo.

Examples in the
[java-sdk repo](https://github.com/clutchcall/java-sdk/tree/main/examples).

## Voice — outbound originate

```java
var v    = new Voice("https://app.clutchcall.dev", apiKey, "org_abc");
var call = v.calls().originate(new Voice.OriginateArgs()
    .to("+15551234567")
    .from("+15558675309")
    .trunkId("trunk_main")
    .agent("healthcare-assistant"));
System.out.println("call sid: " + call.sid());
```

## Streams — record-to-disk viewer

```java
try (var out = Files.newOutputStream(Path.of("recording.mp4"));
     var viewer = Streams.BroadcastViewer.open(signedUrl,
            (init, chunk) -> out.write(chunk),
            reason -> {})) { }
```

## Robotics — telemetry publisher

```java
var r   = new Robotics("relay.clutchcall.dev", token, "tb-7");
var qos = new Robotics.QoSProfile()
    .reliability(Robotics.Reliability.RELIABLE)
    .depth(10);

try (var pub = r.publishTelemetry("odom", "nav_msgs/msg/Odometry", qos)) {
    while (running) {
        pub.write(serializeOdom());
        Thread.sleep(100);
    }
}
```

## Games — authoritative server

```java
var g     = new Games("relay.clutchcall.dev", token, "duel-42", null);
var state = g.publishState(new Games.StateOpts().tickHz(30));

g.subscribeInputs((pid, p) -> world.apply(pid, p));
var exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(() -> state.write(world.snapshot()),
                         0, 33, TimeUnit.MILLISECONDS);
```

## Data — MQTT-style subscribe

```java
var d = new Data("relay.clutchcall.dev", token, "ingest");
try (var sub = d.subscribe("sensors/+/temperature",
        msg -> System.out.println(
            msg.topic() + " ← " + msg.fromClientId() + " = "
                       + new String(msg.payload())))) {
    // sub is AutoCloseable
}
```
