🏋️♂️
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PostgresPubSub.Repo.Migrations.CreateAccounts do | |
use Ecto.Migration | |
def change do | |
create_if_not_exists table(:accounts) do | |
add(:username, :string, null: true) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PostgresPubSub.Listener do | |
# ... | |
require Logger | |
@impl true | |
def handle_info({:notification, _pid, _ref, "accounts_changed", payload}, _state) do | |
with {:ok, data} <- Poison.decode(payload, keys: :atoms) do | |
data | |
|> inspect() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PostgresPubSub.Listener do | |
use GenServer | |
def child_spec(opts) do | |
%{ | |
id: __MODULE__, | |
start: {__MODULE__, :start_link, [opts]} | |
} | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PostgresPubSub.Repo do | |
# ... | |
def listen(event_name) do | |
with {:ok, pid} <- Postgrex.Notifications.start_link(__MODULE__.config()), | |
{:ok, ref} <- Postgrex.Notifications.listen(pid, event_name) do | |
{:ok, pid, ref} | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PostgresPubSub.Application do | |
use Application | |
def start(_type, _args), | |
do: Supervisor.start_link(children(), opts()) | |
defp children do | |
[ | |
PostgresPubSub.Repo | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PostgresPubSub.Repo do | |
use Ecto.Repo, | |
otp_app: :postgres_pubub | |
def child_spec(opts) do | |
%{ | |
id: __MODULE__, | |
start: {__MODULE__, :start_link, [opts]}, | |
type: :supervisor | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TRIGGER accounts_changed | |
AFTER INSERT OR UPDATE | |
ON accounts | |
FOR EACH ROW | |
EXECUTE PROCEDURE notify_account_changes() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION notify_account_changes() | |
RETURNS trigger AS $$ | |
BEGIN | |
PERFORM pg_notify( | |
'accounts_changed', | |
json_build_object( | |
'operation', TG_OP, | |
'record', row_to_json(NEW) | |
)::text | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MinimalServer.Application do | |
# ... | |
defp children do | |
[ | |
MinimalServer.Endpoint | |
] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MinimalServer.Endpoint do | |
# ... | |
def child_spec(opts) do | |
%{ | |
id: __MODULE__, | |
start: {__MODULE__, :start_link, [opts]} | |
} | |
end |