jakub-zawislak

jakub-zawislak

I’m new to distributed elixir.

I have two clean installations of Phoenix 1.4.2 - apps named A and B. I’m running these with --sname option. I can run command from another application using :rpc.call. Can I subscribe to Phoenix.PubSub from another app?

I have tried:

iex(a@MacBook-Pro)1> Node.connect :"b@MacBook-Pro"
iex(a@MacBook-Pro)2> Phoenix.PubSub.subscribe(B.PubSub, "topic")
# throws argument error - :ets.lookup(B.PubSub, :subscribe)

Should it be somehow wrapped by a GenServer on the B side and used with Node.spawn_link? Is there some easier solution? First I wanted to broadcast messages between two apps installed on same server using websockets but then I thought there should be an easier method.

Showing Posts 1 to 7

blatyo

blatyo

Conduit Core Team

I’m not super familiar with the implementation details, but I’ll try to describe it conceptually and hopefully that’ll help.

Phoenix.PubSub works, by creating a cluster of different nodes. You need to start a Phoenix.PubSub process on every node in the cluster. You can specify an adapter, which allows Phoenix.PubSub to discover what other nodes are available to connect to. In the case of the redis adapter, each node will connect to redis and messages will be sent through that. In the case of PG2 (the default), Phoenix.PubSub will connect with other Phoenix.PubSub processes on any of the nodes already clustered by the nodes being connected.

So, you’ve correctly connected the nodes. But it looks like you’ve not started a Phoenix.PubSub process. So, when you call Phoenix.PubSub.subscribe/2, the process that would’ve setup stuff locally on the current node hasn’t done so and you’re getting an error about a lookup on an ets table not existing.

I think in the simple iex session you’re just missing something like:

Phoenix.PubSub.PG2.start_link(name: B.PubSub)

jakub-zawislak

jakub-zawislak OP

My apps are clean installations of Phoenix, so they have PubSub enabled by default in config.exs:

config :b, BWeb.Endpoint,
  # ...
  pubsub: [name: B.PubSub, adapter: Phoenix.PubSub.PG2]

I also tried to manually set up another PubSub in application.ex:

defmodule B.Application do
  def start(_type, _args) do
    children = [
      BWeb.Endpoint,
      {Phoenix.PubSub.PG2, name: :another_pubsub},
    ]
    # ...
  end

I can access this PubSub from the B app:

iex(b@MacBook-Pro)1> Phoenix.PubSub.subscribe :another_pubsub, "asd"
:ok

but I still cannot connect from the A app.

blatyo

blatyo

Conduit Core Team

I did some brief digging. The server_name needs to be the same for two PubSub’s to connect via PG2.

https://github.com/phoenixframework/phoenix_pubsub/blob/v1.1.2/lib/phoenix/pubsub/pg2_server.ex#L42-L44

So, the name argument needs to be the same as what it is on node A. You also still need to connect the nodes if you’re not doing that.

jakub-zawislak

jakub-zawislak OP

I figured it out without the Phoenix.PubSub. I think it’s all I need.

Create pg2 group in A

defmodule A.Application do
  def start(_type, _args) do
    :pg2.create :some_group
    # ...
  end

Connect to group in B

defmodule B.Application do
  def start(_type, _args) do
    children = [
      # ...
      {B.Worker, []},
    ]
    # ...
  end
defmodule B.Worker do
  use GenServer

  def start_link(opts) do
    {:ok, pid} = GenServer.start_link(__MODULE__, :ok, opts)

    true = Node.connect(:"a@MacBook-Pro")
    :ok = :pg2.join(:some_group, pid)

    {:ok, pid}
  end
end

Send message in A

iex(a@MacBook-Pro)1> [pid] = :pg2.get_members :some_group
iex(a@MacBook-Pro)2> send(pid, :hello) 

Handle messages from A in B

defmodule B.Worker do
  def handle_info(msg, state) do
    IO.inspect msg

    {:noreply, state}
  end
end
jakub-zawislak

jakub-zawislak OP

Maybe someone will be interested in my final implementation of the subscriber module

defmodule B.Worker do
  use GenServer

  @reconnect_interval 1_000

  def start_link(opts) do
    GenServer.start_link(__MODULE__, :ok, opts)
  end

  def init(:ok) do
    Process.send_after(self(), :join_pg2, 0)

    {:ok, %{}}
  end

  def handle_info(:join_pg2, state) do
    node_name = :"a@MacBook-Pro"

    case Node.connect(agent_node) do
      true ->
        if :group_name not in :pg2.which_groups() do
          :global.sync()
          :ok = :pg2.join(:group_name, self())
        end

        Node.monitor(node_name, true)

        {:noreply, state}

      false ->
        Process.send_after(self(), :join_pg2, @reconnect_interval)

        {:noreply, state}
    end
  end

  # from Node.monitor
  def handle_info({:nodedown, _node}, state) do
    Process.send_after(self(), :join_pg2, 0)

    {:noreply, state}
  end

  def handle_info(msg, state) do
    IO.inspect msg

    {:noreply, state}
  end
end

blatyo

blatyo

Conduit Core Team

You can just do

send(self(), :join_pg2)

lkuty

lkuty

I invite you to have a look at Distributed Phoenix Chat with PubSub PG2 adapter.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews