> ## Documentation Index
> Fetch the complete documentation index at: https://docs.x.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming

> Consume real-time X API v2 streams and manage filtered stream rules in Java.

Streaming methods on `client.stream()` return `EventStream<T>` (`Iterable<T>` and `AutoCloseable`). Use `try (...)` so the underlying HTTP connection closes when you exit the loop.

## Filtered and sampled streams

```java theme={null}
import com.x.xdk.Schemas;
import com.x.xdk.XClient;
import com.x.xdk.clients.StreamClient;
import java.util.List;

XClient client = XClient.builder()
    .bearerToken(System.getenv("X_BEARER_TOKEN"))
    .build();

StreamClient.PostsOptions options = StreamClient.PostsOptions.builder()
    .tweetFields(List.of("created_at", "author_id", "public_metrics"));

try (var stream = client.stream().posts(options)) {
    int count = 0;
    for (Schemas.StreamPostsResponse event : stream) {
        System.out.println(event.getData());
        if (++count >= 25) {
            break;
        }
    }
}
```

## Managing rules

```java theme={null}
import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();

// Add a filter rule
var addPayload = mapper.readTree(
    "{\"add\":[{\"value\":\"from:XDevelopers -is:retweet\",\"tag\":\"xdevs\"}]}"
);
client.stream().updateRules(addPayload);

// List active rules
for (var page : client.stream().getRulesPages()) {
    System.out.println(page.getData());
}
```

See [Build a Rule](https://docs.x.com/x-api/posts/filtered-stream/integrate/build-a-rule.md) for rule syntax and operators.
