Subscriptions are the core of Lark’s real-time capabilities. When data changes on the server, your callback fires with the updated data.
For a conceptual overview of how subscriptions work under the hood (delta sync, shared views), see Subscriptions.
Basic usage
Call ref.on(eventType, callback) to start listening. It returns an unsubscribe function you call when you’re done:
The unsubscribe pattern works well with React’s useEffect cleanup, Vue’s onUnmounted, and similar framework lifecycle hooks.
Event types
value
Fires once with the initial data, then again every time anything at the location (or its children) changes. The callback receives the complete snapshot.
child_added
Fires once for each existing child, then again whenever a new child is added. The callback receives the child’s snapshot and the key of the previous child (for ordering).
child_changed
Fires when an existing child’s value changes. Does not fire for additions or removals.
child_removed
Fires when a child is removed.
child_moved
Fires when a child’s sort order changes (due to priority or value changes when using ordered queries).
Practical examples
Player list
Keep a live roster of players in a multiplayer game:
Chat messages
Listen for messages added to a chat room:
Live game score
Track a single value that changes frequently:
Leaderboard
Watch a sorted, limited query:
Unsubscribing
Always unsubscribe when you no longer need updates. This frees up resources on both the client and server.
Forgetting to unsubscribe is a common source of memory leaks and unexpected behavior. Always clean up your listeners when a component unmounts or a view is no longer active.
React example