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.
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?
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", {});