Skip to content

Commit

Permalink
fix: Update README with subscribe tuple changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cheerfulstoic committed Aug 7, 2024
1 parent c7c3b4b commit b0e3776
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ This will setup:
Then any process (e.g. a GenServer, a LiveView, a Phoenix channel, etc...) can subscribe to messages like so:

```elixir
EctoWatch.subscribe(User, :inserted)
EctoWatch.subscribe(User, :updated)
EctoWatch.subscribe(User, :deleted)
EctoWatch.subscribe({User, :inserted})
EctoWatch.subscribe({User, :updated})
EctoWatch.subscribe({User, :deleted})

EctoWatch.subscribe(Package, :inserted)
EctoWatch.subscribe(Package, :updated)
EctoWatch.subscribe({Package, :inserted})
EctoWatch.subscribe({Package, :updated})
```

(note that if you are subscribing in a LiveView `mount` callback you should subscribe inside of a `if connected?(socket) do` to avoid subscribing twice).

You can also subscribe to individual records:

```elixir
EctoWatch.subscribe(User, :updated, user.id)
EctoWatch.subscribe(User, :deleted, user.id)
EctoWatch.subscribe({User, :updated}, user.id)
EctoWatch.subscribe({User, :deleted}, user.id)
```

... OR you can subscribe to records by an association column (but the given column must be in the `extra_columns` list for the watcher! See below for more info on the `extra_columns` option):

```elixir
EctoWatch.subscribe(User, :updated, {:role_id, role.id})
EctoWatch.subscribe(User, :deleted, {:role_id, role.id})
EctoWatch.subscribe({User, :updated}, {:role_id, role.id})
EctoWatch.subscribe({User, :deleted}, {:role_id, role.id})
```

Once subscribed, messages can be handled like so (LiveView example are given here but `handle_info` callbacks can be used elsewhere as well):
Expand Down Expand Up @@ -173,7 +173,7 @@ If you would like to get more than just the `id` from the record, you can use th
]}

# subscribing
EctoWatch.subscribe(Comment, :deleted)
EctoWatch.subscribe({Comment, :deleted})

# handling messages
def handle_info({{Comment, :deleted}, %{id: id, post_id: post_id}}, socket) do
Expand Down

0 comments on commit b0e3776

Please sign in to comment.