↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

How to use Postgres NOTIFY PubSub with Elixir/Phoenix

The post explains how to use Postgres NOTIFY in a Phoenix application so the app can react when a database row changes. It gives examples like sending a welcome email for new members or keeping two systems in sync.

Reading notes
#

  • NOTIFY sends a notification event with an optional payload to clients that previously used LISTEN on the same channel.
  • The feature is presented as a simple interprocess communication mechanism for processes using the same PostgreSQL database.
  • A payload string can travel with the notification, and more structured data can be built with tables that pass extra data from notifier to listeners.
  • The author says the application can call a function every time a row changes on a specific table.
  • The example uses the phoenix_postgres_pub_sub package and an example Phoenix application.
  • The setup starts with creating a new Phoenix app and generating a User schema under the Accounts context.
  • The instructions include running the database setup and adding the dependency to mix.exs.
  • The config file is updated with adapter and repo settings for phoenix_postgres_pub_sub.
  • A module is created to receive notifications and print each one with IO.inspect.
  • A migration generates the Postgres function and trigger used to dispatch notifications for the users table.
  • The worker is added to the application supervision tree with a string that combines the table name and changes.
  • The post says the same pattern can monitor other tables by passing other table_changes strings.
  • The author links to a demo project on GitHub and invites comments with questions.