DEV Community

Mạnh Vũ
Mạnh Vũ

Posted on • Edited on

6

Using Phoenix.PubSub as a message-bus for Elixir cluster

In my sharing session about using Phoenix.PubSub as message-bus for Elixir cluster, it quite simple and effective way for small & medium system. Now I recap it.

bus-message between two processes of two nodes
(Simple case for using Phoenix PubSub to create a bus-message for two or more processes in different nodes)

For standalone Elixir app (without Phoenix framework), we need add depend libraries to mix file:



  defp deps do
    [
      {:phoenix_pubsub, "~> 2.1"},
      {:libcluster, "~> 3.3"}
    ]
  end


Enter fullscreen mode Exit fullscreen mode

:phoenix_pubsub is a library from Phoenix framework, it can run without framework.
:libcluster is a new way to run Elixir app in cluster by declare config in config.exs



config :libcluster,
  topologies: [
    local_epmd: [
      # The selected clustering strategy. Required.
      strategy: Cluster.Strategy.LocalEpmd,
      # Configuration for the provided strategy. Optional.
      config: [hosts: [:"frontend_1@127.0.0.1", :"front_end_2@127.0.0.1", :"trading@127.0.0.1"]],
      # The function to use for connecting nodes. The node
      # name will be appended to the argument list. Optional
      connect: {:net_kernel, :connect_node, []},
      # The function to use for disconnecting nodes. The node
      # name will be appended to the argument list. Optional
      disconnect: {:erlang, :disconnect_node, []},
      # The function to use for listing nodes.
      # This function must return a list of node names. Optional
      list_nodes: {:erlang, :nodes, [:connected]},
    ]]


Enter fullscreen mode Exit fullscreen mode

This is simple config for running cluster by :libcluster

In Phoenix app just add :libcluster & its config are enough.

In Application module in app need to add PubSub as a child process to application supervisor (or your supervisor)



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


Enter fullscreen mode Exit fullscreen mode

In process need to receive message from message-bus we need to run subscribe to subscribe a topic on a PubSub like:



PubSub.subscribe(pubsub_name, topic_name)


Enter fullscreen mode Exit fullscreen mode

pubsub_name is name of PubSub we create in supervisor.
topic_name is name of topic in PubSub we want
to receive messages.

When have a message we will receive it as a message in our process or handle_infoif we used GenServer.

After done with PubSub we need to unsubscribe to remove process out of PubSub (in case we don't remove, messages will be dropped if our process exited).

If want to send message to bus we need to use broadcast function to send messages:



PubSub.broadcast(pubsub_name, topic, {:join,  my_id, "Hello"})


Enter fullscreen mode Exit fullscreen mode

{:join, my_id, "Hello"} is our message.

Now, we can send message around cluster with a little effort.

Conclusion
:libcluster help us join apps to cluster just by add config.
:phoenix_pubsub help us send message cross cluster.

Source available at our Github repo

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay