Subscribing to external topics on Phoenix

According to, 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

On the client

const channel =`game:${game_id}`, {});
1 Like