Elmseld

Elmseld

How do I make two Elixir applications(server & client) communicate with Phoenix.PubSub?

I have two elixir-applications, that need to communicate with each other:

  • 1 Server that has a PubSub with 2 topics, it broadcasts to one topic and listens to the other topic.
  • 1 Client, that should subscribe to the Servers PubSub topic and broadcast to the other topic.

The scenario I want to get is that I start the Server, and then start one or multiple Clients that can subscribe to the server.

It just needs to work locally in the terminal, so it doesn’t need any fancy.

The Server uses :phoenix_pubsub, "~> 2.1" and the rest is mostly a Genserver for message-handling.

The client is basically just a Genserver that only needs to subscribe to the server and sends A message when it receives one.

Any tips on how I can make them talk?

Marked As Solved

kokolegorille

kokolegorille

Hello and welcome,

You could add pub_sub as a dependency in the client, then add it to the client supervision tree with the same name as the one in Phoenix.

You need to cluster both nodes. I use libcluster for this, but You have other options.

Then, it should work… :slight_smile:

For example

$ mix new demo --sup
$ cd demo
# add deps in mix.exs
# {:phoenix_pubsub, "~> 2.1"},
# start pub_sub in lib/demo/application.ex
# {Phoenix.PubSub, name: Demo.PubSub},
$ mix deps.get

Start first node and subscribe to pub_sub…

$ iex --sname demo1 --cookie cookie -S mix
iex(demo1@arakis)1> Phoenix.PubSub.subscribe(Demo.PubSub, "topic")
:ok

Start second node in another console, connect both nodes, and broadcast a message…

$ iex --sname demo2 --cookie cookie -S mix
iex(demo2@arakis)1> Node.ping :demo1@arakis
:pong
iex(demo2@arakis)2> Node.list
[:demo1@arakis]
iex(demo2@arakis)3> Phoenix.PubSub.broadcast(Demo.PubSub, "topic", %{message: "Hey there!"})
:ok

Receive message in first node…

iex(demo1@arakis)2> flush
%{message: "Hey there!"}
:ok

Last Post!

byronsalty

byronsalty

I wonder if you’re missing a piece in your topology to assign your docker containers to a shared network. I’ve always had issues with separate containers talking to each other unless I explicitly set a network.

In my case, I wasn’t using docker-compose but starting docker manually.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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

We're in Beta

About us Mission Statement