Skip to main content

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 leaks

Conditional 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

Always clean up subscriptions - Call .off() in cleanup functions to prevent memory leaks and unexpected behavior.

Store references

Framework integration

Common patterns

Conditional unsubscribe

Multiple subscriptions

Resubscribe pattern

Troubleshooting

Memory leaks

Forgetting to call .off() causes memory leaks. Symptoms include:
  • Increasing memory usage over time
  • Multiple callbacks firing for the same event
  • Slow performance after navigation

Double subscription

See also

.on()

Subscribe to realtime updates

.once()

Read data one time without subscribing