---
title: "Discussion on \"Distributed PubSub in Elixir\" | Hashnode"
description: "Discussion on \"Distributed PubSub in Elixir\". A writeup on building a distributed PubSub in Elixir. From local-only broadcasts to clustered nodes."
url: "https://hashnode.com/posts/distributed-pubsub-in-elixir/66f6a8c06c31faba8f95fef0"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Distributed PubSub in Elixir](https://papers.vincy.dev/distributed-pubsub-in-elixir?utm_source=hashnode&utm_medium=feed)

A writeup on building a distributed PubSub in Elixir. From local-only broadcasts to clustered nodes.

Sep 27, 2024 · 10 min read · papers.vincy.dev  
Read the article

-   [#elixir](https://hashnode.com/tag/elixir)
-   [#microservices](https://hashnode.com/tag/microservices)
-   [#phoenix](https://hashnode.com/tag/phoenix)
-   [#erlang](https://hashnode.com/tag/erlang)
-   [#pubsub](https://hashnode.com/tag/pubsub)

## Written by

[Vince Urag](https://hashnode.com/@vinceurag)

@vinceurag · Manila, Philippines

  
[View profile](https://hashnode.com/@vinceurag)

I tinker with Elixir.

I'm a software engineer with 7+ years experience in backend engineering. My main focus is on distributed systems, concurrency, parallelism, etc. I usually dabble with Elixir/Erlang.

## Responses 1

Markdown supported · ⌘↵ to post

[Dan dan](https://hashnode.com/@dandan00dan)

[Oct 16, 2024](https://hashnode.com/posts/distributed-pubsub-in-elixir/66f6a8c06c31faba8f95fef0/comment/67101c0a9fbb4682564788a6)

I'm still a beginner, but I was wondering why :pg is used to get all the PIDs of the PGServer processes in the cluster and send them messages (e.g., {:broadcast\_to\_local, topic, message}). Why not use something like Enum.each(Node.list(), fn node -> Hermes.broadcast\_local(topic, message) end) instead? I'm sure there's a good reason, I'm just trying to understand how it all works.

[Vince Urag](https://hashnode.com/@vinceurag)

[Oct 17, 2024](https://hashnode.com/posts/distributed-pubsub-in-elixir/66f6a8c06c31faba8f95fef0/comment/67101c0a9fbb4682564788a6#reply-6710b0f3d8f93ae73494a18e)

Hello Dan, you are right, you can also achieve this behavior by sending directly to nodes on simple scenarios. Although, :pg has more stuff you could probably use like scoping. One issue I'm seeing in this approach is the caller will be the one to decide which node will receive the message instead of allowing nodes to decide whether they want to receive messages or not. For example, you have a cluster of nodes. If you want to use Node.list + Node.spawn + Hermes.boradcast\_local, you need to make sure that all nodes in the cluster has `Hermes`. If you use :pg, a node that wants to be part of the pubsub just needs to have a PGServer running. If the node doesn't want to participate, the node can still be part of the cluster, even without PGServer running.
