see on GitHub

How to consume a data subscription?

Subscriptions are consumed by the client on top of a continuous TCP connection. Those TCP connections are managed on the client side by s Subscription object, In this page we'll cover how to get those objects and how to use the to consume the subscription.

Simple consumption example

First, let us start from real simple subscription consumption example: Here we create a subscription and have a worker processing documents

public async Task Worker(CancellationToken cancellationToken)
	    {
 IDocumentStore store = new DocumentStore();
    
 var subscriptionName = await store.Subscriptions.CreateAsync<Order>(x => x.Company == "companies/11");
 var subscription = store.Subscriptions.GetSubscriptionWorker<Order>(subscriptionName);
 var subscriptionTask = subscription.Run(x =>
     x.Items.ForEach(item =>
         Console.WriteLine($"Order #{item.Result.Id} will be shipped via: {item.Result.ShipVia}")), 
            cancellationToken);	        
 await subscriptionTask;
	    }

Creating subscription worker object

The subscription worker object manages the subscription on the client side. Not that upon creation of the object, no connection will be created. A connection is created only when the received object's Run method is called.

Get typed subscription worker

Here we get a subscription worker, only based on subscription name.
subscription = store.Subscriptions.GetSubscriptionWorker<Order>(name);

Get typed subscription worker

Here we get a subscription worker, which in the case of another client consuming the connection, will wait for it to finish on the server side and only then start consuming the subscription.
subscription = store.Subscriptions.GetSubscriptionWorker<Order>(new SubscriptionWorkerOptions(name)
{
    Strategy = SubscriptionOpeningStrategy.WaitForFree
});

Get typed subscription worker

Here we get a subscription worker as before, but also we set the maximum batch size to 500. And we define that if there is an error during the client's Run function, we will not abort the worker processing as defined by default, but we will continue processing.
subscription = store.Subscriptions.GetSubscriptionWorker<Order>(new SubscriptionWorkerOptions(name)
{
    Strategy = SubscriptionOpeningStrategy.WaitForFree,
       MaxDocsPerBatch = 500,
       IgnoreSubscriberErrors = true
});

This page is a work in progress