Changes API: How to Subscribe to Counter Changes

Following methods allow you to subscribe to counter changes:

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
counterName String Name of a counter to subscribe to.
Return Value
IChangesObservable<CounterChange> Observable that allows to add subscriptions to counter notifications.

Example

store
    .changes()
    .forCounter("likes")
    .subscribe(Observers.create(change -> {
        switch (change.getType()) {
            case 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
documentId String ID of a document to subscribe to.
counterName String Name of a counter to subscribe to.
Return Value
IChangesObservable<CounterChange> Observable that allows to add subscriptions to counter notifications.

Example

store
    .changes()
    .forCounterOfDocument("companies/1-A", "likes")
    .subscribe(Observers.create(change -> {
        switch (change.getType()) {
            case INCREMENT:
                // do something
                break;
        }
    }));

ForCountersOfDocument

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

Syntax

IChangesObservable<CounterChange> forCountersOfDocument(String documentId);
Parameters
documentId String ID of a document to subscribe to.
Return Value
IChangesObservable<CounterChange> Observable that allows to add subscriptions to counter notifications.

Example

store
    .changes()
    .forCountersOfDocument("companies/1-A")
    .subscribe(Observers.create(change -> {
        switch (change.getType()) {
            case 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

store
    .changes()
    .forAllCounters()
    .subscribe(Observers.create(change -> {
        switch (change.getType()) {
            case INCREMENT:
                // do something ...
                break;
        }
    }));

CounterChange

Name Type Description
Type CounterChangeTypes Counter change type enum
Name String Counter name
Value Long Counter value after the change
DocumentId String Counter document identifier
ChangeVector String Counter's ChangeVector

CounterChangeTypes

Name Value
NONE 0
PUT 1
DELETE 2
INCREMENT 4