Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
resume token demo for watcher/change-stream
using MongoDB.Bson;
using MongoDB.Entities;
using System;
using System.IO;
using System.Threading.Tasks;
namespace TestApplication
{
public class Book : Entity
{
public string Title { get; set; }
}
public static class Program
{
private static async Task Main()
{
await DB.InitAsync("test");
var watcher = DB.Watcher<Book>("book-watcher");
watcher.OnChanges += books =>
{
foreach (var book in books)
{
Console.WriteLine($"received: {book.Title}");
}
};
if (File.Exists("resume.token"))
{
var resumeToken = BsonDocument.Parse(File.ReadAllText("resume.token"));
watcher.StartWithToken(resumeToken, EventType.Created, batchSize: 10);
}
else
{
watcher.Start(EventType.Created, batchSize: 10);
for (int i = 1; i <= 20; i++)
{
await new Book { Title = $"book {i}" }.SaveAsync();
await Task.Delay(500);
if (i == 10)
{
File.WriteAllText("resume.token", watcher.ResumeToken.ToJson());
}
}
}
Console.WriteLine("press any key to exit...");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment