Overview
The.off() method unsubscribes from realtime updates by removing event listeners. This is essential for preventing memory leaks and stopping unwanted callbacks when you no longer need to listen to data changes.
Syntax
Usage
Basic unsubscribe
Event object pattern
Clean up on component unmount
Implementation details
From~/workspace/source/src/on.js:51:
When to use .off()
Component unmount
Always call
.off() when components unmount to prevent memory leaksConditional listening
Stop listening when certain conditions are met (e.g., data is complete)
Route changes
Unsubscribe when navigating away from a page or view
Resource cleanup
Clean up subscriptions before creating new ones on the same reference
Comparison: .on() vs .once()
Best practices
Store references
Framework integration
Common patterns
Conditional unsubscribe
Multiple subscriptions
Resubscribe pattern
Troubleshooting
Memory leaks
Double subscription
See also
.on()
Subscribe to realtime updates
.once()
Read data one time without subscribing