Connect
Attach to an existing Live session. Do not send session.start again.
WebSocket wss://api.openai.com/v1/live/sessions/{session_id}/attach
Connect
import com.openai.client.okhttp.OkHttpClient;
import com.openai.core.ClientOptions;
import com.openai.helpers.LiveSidebandConnection;
import com.openai.models.live.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
class ConnectExample {
public static void main(String[] args) throws Exception {
ClientOptions options =
ClientOptions.builder().fromEnv().httpClient(OkHttpClient.builder().build()).build();
try {
// Use an eligible existing Live session ID owned by your project.
// This scope detaches only the observer; it never closes the owner's session.
try (LiveSidebandConnection connection =
LiveSidebandConnection.connect(options, "sess_123")) {
// One deadline for the exchange; unrelated events must not extend it.
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(60);
while (true) {
long remaining = deadline - System.nanoTime();
if (remaining <= 0) {
throw new TimeoutException("Live event deadline expired after 60 seconds.");
}
ServerEvent event = connection.receiveAsync().get(remaining, TimeUnit.NANOSECONDS);
if (event.isErrorEvent()) {
throw new IllegalStateException("Live reported a session error.");
} else if (event.isSessionUsageUpdated()) {
System.out.println(
"Current cumulative usage: " + event.asSessionUsageUpdated().usage());
break;
} else if (event.isSessionClosed()) {
System.out.println("Final usage: " + event.asSessionClosed().usage());
break;
}
}
}
} finally {
options.close();
}
}
}