Skip to content

Commit 54eba34

Browse files
docs: add more examples
1 parent d9e462c commit 54eba34

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,98 @@ CompletableFuture<SessionActResponse> response = client.sessions().act(params);
190190

191191
The asynchronous client supports the same options as the synchronous one, except most methods return `CompletableFuture`s.
192192

193+
## Streaming
194+
195+
The SDK defines methods that return response "chunk" streams, where each chunk can be individually processed as soon as it arrives instead of waiting on the full response. Streaming methods generally correspond to [SSE](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) or [JSONL](https://jsonlines.org) responses.
196+
197+
Some of these methods may have streaming and non-streaming variants, but a streaming method will always have a `Streaming` suffix in its name, even if it doesn't have a non-streaming variant.
198+
199+
These streaming methods return [`StreamResponse`](stagehand-java-core/src/main/kotlin/com/browserbase/api/core/http/StreamResponse.kt) for synchronous clients:
200+
201+
```java
202+
import com.browserbase.api.core.http.StreamResponse;
203+
import com.browserbase.api.models.sessions.StreamEvent;
204+
205+
try (StreamResponse<StreamEvent> streamResponse = client.sessions().actStreaming(params)) {
206+
streamResponse.stream().forEach(chunk -> {
207+
System.out.println(chunk);
208+
});
209+
System.out.println("No more chunks!");
210+
}
211+
```
212+
213+
Or [`AsyncStreamResponse`](stagehand-java-core/src/main/kotlin/com/browserbase/api/core/http/AsyncStreamResponse.kt) for asynchronous clients:
214+
215+
```java
216+
import com.browserbase.api.core.http.AsyncStreamResponse;
217+
import com.browserbase.api.models.sessions.StreamEvent;
218+
import java.util.Optional;
219+
220+
client.async().sessions().actStreaming(params).subscribe(chunk -> {
221+
System.out.println(chunk);
222+
});
223+
224+
// If you need to handle errors or completion of the stream
225+
client.async().sessions().actStreaming(params).subscribe(new AsyncStreamResponse.Handler<>() {
226+
@Override
227+
public void onNext(StreamEvent chunk) {
228+
System.out.println(chunk);
229+
}
230+
231+
@Override
232+
public void onComplete(Optional<Throwable> error) {
233+
if (error.isPresent()) {
234+
System.out.println("Something went wrong!");
235+
throw new RuntimeException(error.get());
236+
} else {
237+
System.out.println("No more chunks!");
238+
}
239+
}
240+
});
241+
242+
// Or use futures
243+
client.async().sessions().actStreaming(params)
244+
.subscribe(chunk -> {
245+
System.out.println(chunk);
246+
})
247+
.onCompleteFuture();
248+
.whenComplete((unused, error) -> {
249+
if (error != null) {
250+
System.out.println("Something went wrong!");
251+
throw new RuntimeException(error);
252+
} else {
253+
System.out.println("No more chunks!");
254+
}
255+
});
256+
```
257+
258+
Async streaming uses a dedicated per-client cached thread pool [`Executor`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html) to stream without blocking the current thread. This default is suitable for most purposes.
259+
260+
To use a different `Executor`, configure the subscription using the `executor` parameter:
261+
262+
```java
263+
import java.util.concurrent.Executor;
264+
import java.util.concurrent.Executors;
265+
266+
Executor executor = Executors.newFixedThreadPool(4);
267+
client.async().sessions().actStreaming(params).subscribe(
268+
chunk -> System.out.println(chunk), executor
269+
);
270+
```
271+
272+
Or configure the client globally using the `streamHandlerExecutor` method:
273+
274+
```java
275+
import com.browserbase.api.client.StagehandClient;
276+
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
277+
import java.util.concurrent.Executors;
278+
279+
StagehandClient client = StagehandOkHttpClient.builder()
280+
.fromEnv()
281+
.streamHandlerExecutor(Executors.newFixedThreadPool(4))
282+
.build();
283+
```
284+
193285
## Raw responses
194286

195287
The SDK defines methods that deserialize responses into instances of Java classes. However, these methods don't provide access to the response headers, status code, or the raw response body.

0 commit comments

Comments
 (0)