↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

How to work with multiple Channels in Phoenix?

The discussion is about a Phoenix app where a user needs to receive and send messages on three topics: chat, news, and stock exchange. Ivan asks whether the user info can be shared between topics through the socket struct or whether the user must join all three topics separately.

Chris McCord answers that the client needs to join the three topics. He says broadcasts can go to any topic, but the client only receives events from topics it has joined. He also explains that socket params and channel params can be used together so shared data like a user token is available in each join, and he notes that later Phoenix versions add helper functions for generating and verifying channel tokens and data.

Reading notes
#

  • The user wants one connection to handle chat, news, and stock exchange topics.
  • The question is whether user data can be shared through the socket struct across those topics.
  • Chris says the client must join every topic it wants to receive events from.
  • Broadcasts can target any topic with broadcast topic, event, payload.
  • Socket params and channel params are merged in each join/3.
  • The example uses a user token in socket params and extra channel data in channel params.
  • Chris says future Phoenix versions will add helper functions to generate and verify channel tokens and data.
  • The thread also points Ivan to the Phoenix mailing list for the question.