sezaru

sezaru

Create different pub_sub topics if field is set

I have a resource that I want to send notifications via pub_sub when a new one is created.

In this resource, I have a relationship with my User resource:

    belongs_to :user, Core.Marketplace.Accounts.User do
      allow_nil? true
      public? true
    end

Here is how the pub_sub part is configured:

  pub_sub do
    module Phoenix.PubSub
    name Core.PubSub

    prefix "notifications"

    publish :create, ["new", [:user_id, nil]]
  end

What I want to achieve is that, if the user_id field is non-nil when create is called, I want a notification with the topic: notifications:new:<user_id> and, when the user_id field is nil, I want a notification with the topic: notifications:new.

The issue I’m having is that, with my current pub_sub config, Both topics will be generated if created is called with a user_id.

Is there some good way to achieve that with the pub_sub DSL?

Marked As Solved

sezaru

sezaru

Found a solution right after posting.. you can setup a filter with the publish DSL, so I changed my pub_sub to this:

publish :create, ["new", :user_id], filter: fn %{data: data} -> not is_nil(data.user_id) end

publish :create, ["new", nil], filter:   fn %{data: data} -> is_nil(data.user_id) end

Where Next?

Popular in Questions Top

New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement