Pubsub functionality implementation in Phoenix project

I am seeking a proper way to implement pubsub functionality in phoenix project. I found the documentation help for pubsub but still couldn’t find a proper way to implement it.
Seeking Guidance.



if you mean this doc, so you don’t need more information to start with PubSub in Phoenix. Really, it will just work with this code.

But I think you are asking about what you can do next, you can do whatever you want to do. You can give us some more information what you want to do with PubSub in app with Phoenix framework.


Actually I want to build the pubsub functionality in my simple pheonix project. Which I want to connect with multiple clients on same time using pubsub.
The doc aren’t so briefed. I don’t know where to implement what.
Like I found this one so much helpful. (Phoenix PubSub)
But right after implementing the same I got this error:

Can't find module or function named start_link/3

However start_link funtion is defined in the shopping_list.ex file as written there.

Like I found this one so much helpful. (Phoenix PubSub)
But right after implementing the same I got this error:

because that article is old.

In article:

children = [
    id: Phoenix.PubSub.PG2,
    start: {Phoenix.PubSub.PG2, :start_link, [:fridge, []]}

and based on docs:

children = [{Phoenix.PubSub, name: :fridge}]

So you have to compare what is in old article and what was updated in docs. But maybe you can check also this screencast from ElixirCasts

1 Like

This screencast is paid ones. And I’m a student. That’s why I seeking a method for implementing it.

Hi @lalarukh just to be clear, are you looking to use Phoenix pubsub or implement a pubsub from scratch? If you’re simply looking to use Phoenix PubSub you don’t really need to start anything, you can simply use MyAppWeb.Endpoint.subscribe/1 and broadcast/3. Here is an example from my own app

iex(1)> CoffeeTimeUiWeb.Endpoint.subscribe("test")
iex(2)> CoffeeTimeUiWeb.Endpoint.broadcast("test", "message", %{hello: "world"})
iex(3)> flush()
  topic: "test",
  event: "message",
  payload: %{hello: "world"}

Ok, so try this one Blog · Elixir School

that looks very easy but I think she will be lost because she need to implement GenServer logic as well, or something similar which will read messages. So that can be confused.

@lalarukh are you familiar with GenServers and messages? If not check this GenServer - The Elixir programming language or other parts on this great page.

Does this mean that pubsub is already implemented automatically inside my project? And in future if different clients will be connected with it as it’ll be server then they publish/subscribe to a topic?
I don’t know how my to publish or subscribe to a topic.
I want to implement pubsub from scratch.

Yes, I checked this one before (Blog · Elixir School). But the file code it has like MyAppWeb.GithubdeployView will be linked as pubsub functionality?
And it has a line code inside like this: # our app doing some work, details omitted.

I don’t know what to code inside GithubClient file. Has a lot of confusion regarding this.

If you have a Phoenix project, yes

What clients? How are those clients connected? You haven’t actually described what you’re doing yet.

As a learning exercise? If so, I’d start here: NoRedInk – Pub/Sub in 30 Lines of Elixir


You can connect (subscribe) to PubSub only from Elixir app. So you have to have some running process which will be connected as client to some topic. “PubSub” will send message to your process with broadcast function. That’s all. Flush “command” means that show me all messages for my current process, it’s console in your screenshot. In real use case you have to have own running process, like GenServer or something else which exist as process.

If you want to have connected outside applications as clients, you have to have interface for it, for example Websockets. Channels — Phoenix v1.6.15 It’s based on PubSub and you can connect simple JS on your Phoenix’s pages, other pages or even applications as iOs or Android. It’s not very hard to use it.

Well, first, implementing PubSub is pretty easy, as we wrote already, it’s just a few lines of code. There is nothing else what you have to implement, only these steps:

  1. start PubSub server (adding PubSub to Supervision Tree if it’s not already there)
  2. Subscribe process to some topic
  3. Broadcast messages to some topic
  4. Read messages from topic and do some logic with it (mostly you can use handle_info function to accept incoming message from broadcaster)

BTW: Main logic of PubSub is that PubSub server store information about topics and PIDs of processes which are subscribed to topics. When new message arrive into PubSub, this message is just “forwarded” to these processes/PIDs as regular message. That’s all.

Yes, you’re right. I implemented the channel phx_socket(channel) - phxsocket · PyPI in my client. And have also added the room_channel which is playing as channel in my pheonix server.
Even I’m sending message from client ang getting response from server using phxsocket.

@benwilson512 I’ve a simple phoenix project running channel and python client developed based on phxsocket.
Now, I don’t know those straight steps to implement pubsub in my phoenix server.

so when you have working Channel, you have working PubSub. Channel use it. Now when you want to read or produce messages from Elixir to that “integration” channel for Python, you just have to broadcast message to that Channel’s topic. That’s all.

1 Like

Did you try the link I sent? If so, can you show the code?

1 Like