Subscribing to external topics on Phoenix

According to https://hexdocs.pm/phoenix/1.3.0/Phoenix.Channel.html#module-subscribing-to-external-topics, a server can let a client subscribe topics programmatically.

But if a client don’t know the channels, how they can get the messages from them?
If a client know the channels, why they don’t subscribe them directly?

The client might know the channels, but might not want to use them all at the same time.

You might want to connect to chat channel only when in chat room, and disconnect when leaving it.

Another example would be a game server. You don’t want a game channel, but a channel per game. Allowing the client to connect dynamically to the requested game channel with a proper game_id.

In that case, the client might not know in advance the game_id of the game channel. Only when the game is started…

def join("game:" <> game_id, _params, socket) do
  ...
end

On the client

const channel = socket.channel(`game:${game_id}`, {});
...
channel.join()
1 Like