> ## 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 C#.

Streaming methods on `client.Stream` return `EventStream<T>` (`IAsyncEnumerable<T>` and `IAsyncDisposable`). Use `await using` with `await foreach` so the HTTP connection closes when you exit the loop.

## Filtered and sampled streams

```csharp theme={null}
using System;
using System.Collections.Generic;
using System.Threading;
using Xdk;
using Xdk.Clients;

using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5));
using var client = new XClient(new XClientOptions
{
    BearerToken = Environment.GetEnvironmentVariable("X_BEARER_TOKEN"),
});

var options = new StreamClient.PostsOptions
{
    TweetFields = new List<string> { "created_at", "author_id", "public_metrics" },
};

await using var stream = await client.Stream.PostsStream(options, cts.Token);
int received = 0;
await foreach (var item in stream.WithCancellation(cts.Token))
{
    Console.WriteLine(item.Data);
    if (++received >= 25)
    {
        break;
    }
}
```

## Managing rules

```csharp theme={null}
using System.Text.Json;

using var addDoc = JsonDocument.Parse(
    """{"add":[{"value":"from:XDevelopers -is:retweet","tag":"xdevs"}]}"""
);
await client.Stream.UpdateRulesAsync(addDoc.RootElement);

await foreach (var page in client.Stream.GetRulesPages())
{
    Console.WriteLine(page.Data);
}
```

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