Skip to main content

Changes API: How to Subscribe to Counter Changes

Subscribe to counter changes using ForCounter, ForCounterOfDocument, ForCountersOfDocument, or ForAllCounters.

The Subscribe method on the observable these methods return accepts only an IObserver object by default.
To instead subscribe with a delegate, like the change => ... lambda used in the examples below, add the System.Reactive package to your project.
This adds Subscribe overloads that take delegates, including ones with error and completion handlers.

ForCounter​

Counter changes can be observed using ForCounter method. This will subscribe changes from all counters with a given name, no matter in what document counter was changed.

Syntax​

IChangesObservable<CounterChange> ForCounter(string counterName);

Parameters
counterNamestringName of a counter to subscribe to.
Return Value
IChangesObservable<CounterChange>Observable that allows to add subscriptions to counter notifications.

Example​

IDisposable subscription = store
.Changes()
.ForCounter("Likes")
.Subscribe(
change =>
{
switch (change.Type)
{
case CounterChangeTypes.Increment:
// do something
break;
}
});

ForCounterOfDocument​

Specific counter changes of a given document can be observed using ForCounterOfDocument method.

Syntax​

IChangesObservable<CounterChange> ForCounterOfDocument(string documentId, string counterName);

Parameters
documentIdstringID of a document to subscribe to.
counterNamestringName of a counter to subscribe to.
Return Value
IChangesObservable<CounterChange>Observable that allows to add subscriptions to counter notifications.

Example​

IDisposable subscription = store
.Changes()
.ForCounterOfDocument("companies/1-A", "Likes")
.Subscribe(
change =>
{
switch (change.Type)
{
case CounterChangeTypes.Increment:
// do something
break;
}
});

ForCountersOfDocument​

Counter changes of a specified document can be observed using ForCountersOfDocument method.

Syntax​

IChangesObservable<CounterChange> ForCountersOfDocument(string documentId);

Parameters
documentIdstringID of a document to subscribe to.
Return Value
IChangesObservable<CounterChange>Observable that allows to add subscriptions to counter notifications.

Example​

IDisposable subscription = store
.Changes()
.ForCountersOfDocument("companies/1-A")
.Subscribe(
change =>
{
switch (change.Type)
{
case CounterChangeTypes.Increment:
// do something
break;
}
});

ForAllCounters​

Changes for all counters can be observed using ForAllCounters method.

Syntax​

IChangesObservable<CounterChange> ForAllCounters();

Return Value
IChangesObservable<CounterChange>Observable that allows to add subscriptions to counter notifications.

Example​

IDisposable subscription = store
.Changes()
.ForAllCounters()
.Subscribe(
change =>
{
switch (change.Type)
{
case CounterChangeTypes.Increment:
// do something
break;
}
});

CounterChange and CounterChangeTypes syntax​

The notification object delivered to subscribers when a counter changes.

class CounterChange
{
CounterChangeTypes Type
string Name
long Value
string DocumentId
string CollectionName
string ChangeVector
}

PropertyTypeDescription
TypeCounterChangeTypesCounter change type enum
NamestringCounter name
ValuelongCounter value after the change
DocumentIdstringCounter document identifier
CollectionNamestringDocument's collection name
ChangeVectorstringCounter's ChangeVector

In this article