Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add event feeds to README #215

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,65 @@ if (r.Data.Exists) {

```

## Event Feeds (beta)

The driver supports [Event
Feeds](https://docs.fauna.com/fauna/current/learn/cdc/#event-feeds).

An Event Feed asynchronously polls an [event
source](https://docs.fauna.com/fauna/current/learn/cdc/#create-an-event-source)
for events.

To get paginated events, pass an [event
source](https://docs.fauna.com/fauna/current/learn/cdc/#create-an-event-source)
or a query that produces an event source to `EventFeedAsync()`:

```csharp
// Get an event source from a supported Set
EventSource eventSource = await client.QueryAsync<EventSource>(FQL($"Person.all().eventSource()"));

// Calculate timestamp for 10 minutes ago in microseconds
long tenMinutesAgo = DateTimeOffset.UtcNow.AddMinutes(-10).ToUnixTimeMilliseconds() * 1000;
var feedOptions = new FeedOptions(startTs: tenMinutesAgo, pageSize: 10);

// Pass the event source and `FeedOptions` to `EventFeedAsync()`:
var feed = await client.EventFeedAsync<Person>(eventSource, feedOptions);

// You can also pass a query that produces an event source directly to `EventFeedAsync()`:
var feedFromQuery = await client.EventFeedAsync<Person>(FQL($"Person.all().eventsOn({{ .price, .stock }})"), feedOptions);

// EventFeedAsync() returns a `FeedEnumerable` instance that can act as an `AsyncEnumerator`.
// Use `foreach()` or `NextAsync()` to iterate through the pages of events.

// Example 1: Use `foreach()` to iterate through feed pages.
await foreach (var page in feed)
{
foreach (var evt in page.Events)
{
Console.WriteLine($"Event Type: {evt.Type}");
Person person = evt.Data;
Console.WriteLine($"First Name: {person.FirstName} - Last Name: {person.LastName} - Age: {person.Age}");
}
}

// Example 2: Use `NextAsync()` to manually iterate through feed pages.
// `NextAsync()` requests the next page:
await feed.NextAsync();

if (feedFromQuery.CurrentPage != null && feedFromQuery.CurrentPage.Events.Any())
{
foreach (var evt in feedFromQuery.CurrentPage.Events)
{
Console.WriteLine($"Event Type: {evt.Type}");
Person person = evt.Data;
Console.WriteLine($"First Name: {person.FirstName} - Last Name: {person.LastName} - Age: {person.Age}");
}
}

// Call `NextAsync()` again to fetch the next page:
await feed.NextAsync();
```

## Event Streaming

The driver supports [Event Streaming](https://docs.fauna.com/fauna/current/learn/cdc/#event-streaming).
Expand Down