Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Created February 5, 2021 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj-nitehawk/40b873897f0665af4988ea73b75a937c to your computer and use it in GitHub Desktop.
Save dj-nitehawk/40b873897f0665af4988ea73b75a937c to your computer and use it in GitHub Desktop.
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