phoenix_postgres_pub_sub v0.1.2 PhoenixPostgresPubSub

PhoenixPostgresPubSub

Listen to changes to the tables of your Postgres database. For every INSERT or UPDATE in a specified table, a function will be called with the payload.

Installation

If available in Hex, the package can be installed by adding phoenix_postgres_pub_sub to your list of dependencies in mix.exs:

def deps do
  [
    {:phoenix_postgres_pub_sub, "~> 0.1.0"}
  ]
end

Configuration

In your config.exs add the following:


  config :phoenix_postgres_pub_sub, :config,
    adapter: MainModuleOfYourApp, # this should be the main module of your phoenix application
    repo: MainModuleOfYourApp.Repo # and this should be the main repo of your phoenix application

In your Application Module change the configuration in the following way:

  defmodule MainModuleOfYourApp do

  use Application

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      # Start the endpoint when the application starts
      supervisor(MainModuleOfYourApp.Endpoint, []),
      # Start the Ecto repository
      worker(MainModuleOfYourApp.Repo, []),
      worker(
        PhoenixPostgresPubSub,
        [
          [
            "skills_changes" # this should be always in the format NAME_OF_TABLE_changes,
            "users_changes" # this should be always in the format NAME_OF_TABLE_changes,
            "companies_changes" # this should be always in the format NAME_OF_TABLE_changes,
            ...
          ]
        ],
        restart: :permanent
      ),
      # Here you could define other workers and supervisors as children
      # worker(MainModuleOfYourApp.Worker, [arg1, arg2, arg3]),
    ]

Generate the migration

At this point you have to generate a migration to add the trigger to Postgres. You can do this by running the following command:

mix phoenix_postgres_pub_sub.gen.channel CHOOSE_A_MIGRATION_NAME --table=users
# check the migration file just generated and make your changes if necessary

And run the migration with mix ecto.migration

--table is the table upon which your want to listen to changes

Generate your adapter

Create a module called something like MainModuleOfYourApp.PhoenixPostgresPubSub.

This module should have a function called handle_postgres_notification that accepts two arguments: notification, state

The notification is the one generated by Postgres and sent to this function, while the state is the state of GenServer of PhoenixPostgresPubSub

Test it out

You you have followed all the steps correctly you should be able to make changes to the table on which your are listening to and you should see that the function MainModuleOfYourApp.PhoenixPostgresPubSub.handle_postgres_notification/2 is called every time this happens.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor

Listen for changes

When the GenServer starts subscribe to the given channel

Initialize the GenServer

Link to this section Functions

Link to this function child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function handle_info(msg, state)

Listen for changes

Link to this function init(channels)
init(List.t()) :: {:ok, []}

When the GenServer starts subscribe to the given channel

Link to this function start_link(channels)
start_link(List.t()) :: {:ok, pid()}

Initialize the GenServer