Search documentation of phoenix_kit

# PhoenixKit.Modules.DB.Listener (phoenix_kit v1.7.87)

Copy Markdown View Source

GenServer that listens for PostgreSQL NOTIFY events for live table updates.

This process uses Postgrex to establish a separate connection to PostgreSQL and listens for notifications on the `phoenix_kit_db_changes` channel.

When a notification is received, it broadcasts via PubSub so LiveViews can update in real-time.

## Usage

Since PhoenixKit is a library, the Listener is started lazily on first use. Call `ensure_started/0` before subscribing, or use the subscribe functions which will ensure it's started automatically.

## Summary

### Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

ensure_started()

Ensures the Listener is started. Called automatically by subscribe functions.

start_link(opts \\ [])

Starts the listener process.

subscribe(schema, table)

Subscribe to changes for a specific table.

subscribe_all()

Subscribe to all table changes (for the index page).

unsubscribe(schema, table)

Unsubscribe from changes for a specific table.

unsubscribe_all()

Unsubscribe from all table changes.

## Functions

# child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# ensure_started()

Ensures the Listener is started. Called automatically by subscribe functions.

The Listener is normally started by PhoenixKit.Supervisor. This function is a safety check that logs a warning if the Listener isn't running.

Returns `:ok` if running, or `:ok` with a warning log if not (subscriptions will still work but won't receive notifications until the Listener starts).

# start_link(opts \\ [])

Starts the listener process.

# subscribe(schema, table)

Subscribe to changes for a specific table.

# subscribe_all()

Subscribe to all table changes (for the index page).

# unsubscribe(schema, table)

Unsubscribe from changes for a specific table.

# unsubscribe_all()

Unsubscribe from all table changes.