why

why

Endpoint.broadcast vs Channel.broadcast

I noticed there are two broadcast/3 functions available in different modules:

MyApp.Endpoint.broadcast(topic, event, message)
Docs: “Broadcasts a msg as event in the given topic to all nodes.”

and

MyApp.Channel.broadcast(socket, event, message)
Docs: “Broadcast an event to all subscribers of the socket topic.”

The main difference seems to be in their use of topic vs socket.

I understand the use case of the first (“send a message to all who subscribe to topic XYZ”).

But what might be the practical use case of the second?

Thank you.

First 4 of 4 Posts Switch mode

cenotaph

cenotaph

Documentation explained them succinctly.

When you want to send an update to everyone who wants information about breaking news you would use the endpoint.

When you want to send an update to only subscribers who wants information about ginger cats and reside in cat lovers channel, you would use the second one.

NobbZ

NobbZ

Both do the same. They have been explained to me as “use *.Channel.broadcast if you have the socket, *.Endpoint.broadcast otherwise”.

why

why OP

Interesting, they indeed both broadcast to a topic.

I just looked at the implementation of both, and both use *.Channel.Server.broadcast under the hood.

*.Endpoint:

def broadcast(topic, event, msg) do
        Phoenix.Channel.Server.broadcast(pubsub_server!(), topic, event, msg)
end

*.Channel:

def broadcast(socket, event, message) do
    %{pubsub_server: pubsub_server, topic: topic} = assert_joined!(socket)
    Server.broadcast(pubsub_server, topic, event, message)
end

However, looking at this made me realize that my understanding of how sockets / channels / topics map to one another is incorrect / muddled.

When looking at the above *.Channel implementation as well as how the Socket struct is implemented, I notice that there is a channel key and a topic key (both words singular).

Now, I read somewhere that “multiple channels are multiplexed over one socket”. How would that work with the single channel and single topic keys in socket?

Thanks for any enlightenment :slight_smile:

why

why OP

I think I developed a clearer understanding now of how socket / channel / topic relate to one another.

In the Channels docs it says:

To start communicating, a client connects to a node (a Phoenix server) using a transport (eg, Websockets or long polling) and joins one or more channels using that single network connection. One channel server process is created per client, per topic. The appropriate socket handler initializes a %Phoenix.Socket for the channel server (possibly after authenticating the client). The channel server then holds onto the %Phoenix.Socket{} and can maintain any state it needs within its socket.assigns .

Additionally, this StackOverflow answer also really helps, as it illustrates how to define more than one channel in javascript (here with updated syntax):

// assets/js/socket.js

let channelA = socket.channel("groups:A", {});
let channelB = socket.channel("groups:B", {});
— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

ElixirConf
Beyond Safe Migrations: Better Practices for Ecto + PostgreSQL - Joe Harrow | ElixirConf US 2025 Comments welcome! View t...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement