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 aLiveCounter 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 aLiveCounter 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 thecompact() or compactJson() methods. These methods return the same result as value():
LiveMap, nested LiveCounter objects are included as a number:
Increment a counter
Increment the value of aLiveCounter 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 aLiveCounter 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 thebatch() method. All operations within the batch are sent as one logical unit which succeed or fail together:
Subscribe to updates
Subscribe toLiveCounter 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.
subscribeIterator() method for an async iterator syntax:
