Skip to content

A helper to turn postgres mutations into a pubsub channel

License

Notifications You must be signed in to change notification settings

bnchrch/postgrex_pubsub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PostgrexPubsub

This is a package for easily adding a Postgres based pubsub system to your phoenix application.

Installation

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

def deps do
  [
    {:postgrex_pubsub, "~> 0.2.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/postgrex_pubsub.

Payload Based Usage

1. Apply the Postgres triggers to a table

  1. Create an empy migration
mix ecto.gen.migration broadcast_users_mutation
  1. Use the BroadcastMigration macro
defmodule YourApp.Repo.Migrations.BroadcastUsersMutation do
  use PostgrexPubsub.BroadcastPayloadMigration, table_name: "users"
end
  1. Migrate
mix ecto. migrate

2. Create a listener

defmodule YourApp.Listeners.Email do
  use PostgrexPubsub.Listener, repo: YourApp.Repo

  def handle_mutation_event(%{
    "id" => row_id,
    "new_row_data" => new_row_data,
    "old_row_data" => old_row_data,
    "table" => table,
    "type" => type, # "INSERT", "UPDATE"
  } = payload) do
    IO.inspect(payload, label: "payload")
  end
end

3. Attach the listener

# application.ex
defmodule YourApp.Application do
  use Application

  def start(_type, _args) do
    children = [
      # ...
      YourApp.Listeners.Email,
    ]
    # ...
    Supervisor.start_link(children, opts)
  end

  # ...
end

4. Test it out!

Now when inserting or updating a user you should see the following in your terminal

payload: %{
  "id" => "b3e041a5-2d6e-4f6f-9afc-64f326d3227f",
  "new_row_data" => %{
    "email" => "[email protected]",
    "id" => "b3e041a5-2d6e-4f6f-9afc-64f326d3227f",
    "inserted_at" => "2020-03-30T19:40:17",
    "name" => "Ben Church",
    "stripe_customer_id" => "cus_H0USudjt8o4cuS",
    "updated_at" => "2020-03-30T19:41:16"
  },
  "old_row_data" => %{
    "email" => "[email protected]",
    "id" => "b3e041a5-2d6e-4f6f-9afc-64f326d3227f",
    "inserted_at" => "2020-03-30T19:40:17",
    "name" => "Ben Church",
    "updated_at" => "2020-03-30T19:40:17"
  },
  "table" => "users",
  "type" => "UPDATE"
}

ID Based Usage

Usefull as pg_notify has a hard limit of 8000 bytes

1. Apply the Postgres triggers to a table

  1. Create an empy migration
mix ecto.gen.migration broadcast_users_mutation
  1. Use the BroadcastMigration macro
defmodule YourApp.Repo.Migrations.BroadcastUsersMutation do
  use PostgrexPubsub.BroadcastIdMigration, table_name: "users"
end
  1. Migrate
mix ecto. migrate

2. Create a listener

defmodule YourApp.Listeners.Email do
  use PostgrexPubsub.Listener, repo: YourApp.Repo

  def handle_mutation_event(%{
    "id" => row_id,
    "table" => table,
    "type" => type, # "INSERT", "UPDATE"
  } = payload) do
    IO.inspect(row_id, label: "row_id")
  end
end

3. Attach the listener

# application.ex
defmodule YourApp.Application do
  use Application

  def start(_type, _args) do
    children = [
      # ...
      YourApp.Listeners.Email,
    ]
    # ...
    Supervisor.start_link(children, opts)
  end

  # ...
end

4. Test it out!

Now when inserting or updating a user you should see the following in your terminal

row_id: %{
  "id" => "b3e041a5-2d6e-4f6f-9afc-64f326d3227f",
  "table" => "users",
  "type" => "UPDATE"
}

About

A helper to turn postgres mutations into a pubsub channel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages