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.

2 Likes

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.

1 Like

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

4 Likes

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:

2 Likes

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", {});
4 Likes