Post to PubSub subscribers outside a Phoenix.Channel controller

589 views
Skip to first unread message

simonp...@gmail.com

unread,
Mar 27, 2015, 1:33:10 PM3/27/15
to phoeni...@googlegroups.com
Hi,

I want to know if there is a way to broadcast a message to a topic that will be catch by every subscriber of the topic.
As this issue (https://github.com/phoenixframework/phoenix/issues/671) mentionned, there is no way to list all subscribers anymore.

My use case is simple. An action in a web controller should send a message to a topic and every subscribers should receive it.
I read the docs and tried several things but nothing seems to work :/

Thanks!

Chris McCord

unread,
Mar 27, 2015, 1:41:12 PM3/27/15
to phoeni...@googlegroups.com
That’s exactly what the broadcast functions do today. They broadcast a message on a topic, and all subscribers to that topic receive the message. So it works as you want, but what that issue outline, is you can’t get a list of pids of the subscribers, but you *can* broadcast to all of hem. Make sense?


--
You received this message because you are subscribed to the Google Groups "phoenix-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phoenix-talk...@googlegroups.com.
To post to this group, send email to phoeni...@googlegroups.com.
Visit this group at http://groups.google.com/group/phoenix-talk.
To view this discussion on the web visit https://groups.google.com/d/msgid/phoenix-talk/ed0ac34c-b436-4b1b-a8ce-5cc93cec597e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Kittelson

unread,
Mar 27, 2015, 1:45:51 PM3/27/15
to phoeni...@googlegroups.com
How are you trying to receive the messages? If your subscribing processes are genservers you should be able receive your message via the handle_info callback. To debug you could create a handle_info callback that catches everything to help you catch if the issue is that the messages aren't quite the format you were expecting, e.g.

def handle_info(message, state) do
  # some debugging stuff here, IO.puts, IEx.pry etc  
  {:noreply, state}
end

simonp...@gmail.com

unread,
Mar 27, 2015, 1:53:43 PM3/27/15
to phoeni...@googlegroups.com
Sorry for the noob question but the handle_info should be in the controller? Or on the socket processes directly?

Thanks for the quick responses guys :)

Adam Kittelson

unread,
Mar 27, 2015, 2:10:37 PM3/27/15
to phoeni...@googlegroups.com
I think I misunderstood what you're trying to do, if you're using Channels (as opposed to just PubSub) you can broadcast to those from anywhere with something like:

YourApp.Endpoint.broadcast! "some topic", "some event", %{some: "message"}

I think the info at http://hexdocs.pm/phoenix/Phoenix.Channel.html under "Broadcasting to an external topic" might be what you're looking for if I'm understanding what you're trying to do correctly.

simonp...@gmail.com

unread,
Mar 27, 2015, 2:30:46 PM3/27/15
to phoeni...@googlegroups.com
Ah thanks! That’s exactly what I want to do, I don’t know how I skipped this part of the doc
Reply all
Reply to author
Forward
0 new messages