Skip to main content
LiveCounter is a synchronized numerical counter that supports increment and decrement operations. It ensures that all updates are correctly applied and synchronized across clients in realtime, preventing inconsistencies when multiple clients modify the counter value simultaneously. You interact with LiveCounter through a PathObject or by obtaining a specific Instance.

Create a counter

Create a LiveCounter using the LiveCounter.create() static method and assign it to a path:
LiveCounter.create() returns a value type that describes the initial value for a new counter. The actual object is created when assigned to a path. Each assignment creates a distinct object with its own unique ID:

Get counter value

Access a LiveCounter through a PathObject for path-based operations, or obtain a specific Instance to work with the underlying object directly. Use the value() method to get the value of the LiveCounter:

Get compact object

Get a numeric representation of the counter using the compact() or compactJson() methods. These methods return the same result as value():
When calling these methods on a LiveMap, nested LiveCounter objects are included as a number:

Increment a counter

Increment the value of a LiveCounter using the increment() method. You can specify an amount to increment by, or use the default increment of 1:

Decrement a counter

Decrement the value of a LiveCounter using the decrement() method. You can specify an amount to decrement by, or use the default decrement of 1:

Batch multiple operations

Group multiple counter operations into a single atomic message using the batch() method. All operations within the batch are sent as one logical unit which succeed or fail together:

Subscribe to updates

Subscribe to LiveCounter updates to receive realtime notifications when the value changes. PathObject subscriptions observe a location and automatically track changes even if the LiveCounter instance at that path is replaced. Instance subscriptions track a specific LiveCounter instance, following it even if it moves in the channel object.
Alternatively, use the subscribeIterator() method for an async iterator syntax: