0

I have a lot of channels doing different things like the following:

starting_channel
game_channel
food_channel
chat_channel

And there's maybe 100 of them.

Everyone starts by joining starting_channel. Later I can subscribe the socket to other channel topics and broadcast to different topics accordingly. But the thing is I would not be able to reuse the handle_in functionalities in those channels because users don't actually join them in the first place.

For example, let's say room_channel has this handler:

def handle_in("show_room_happy_users", _payload, socket) do
  broadcast! socket, "happy_users", Presence.list(socket)
  {:noreply, socket}
end

But since you didn't join this channel when you send show_room_happy_users message through the starting_channel nothing would happen.

Is there a way to achieve this without joining all the channel at once?

Shih-Min Lee
  • 9,350
  • 7
  • 37
  • 67
  • not sure I understand your question. Do you want to use the `handle_in` for a channel that the user did not join? Eg, you join just `starting_channel`, but you want to use a `handle_in` from `game_channel`? Can you please give a more detailed example of what you want to achieve? – iacobSon Oct 09 '17 at 07:07
  • @iacobSon I have added an example about what I am trying to achieve. Thanks. – Shih-Min Lee Oct 09 '17 at 14:55
  • Personally, I don't think that is possible, and I don't see how this would work. If you create a channel: `const channel = socket.channel("starting_channel");` `channel.join` then in the front-end you listen for that channel reply: `channel.on(.....)` You cannot receive an answer without creating a channel. – iacobSon Oct 09 '17 at 16:15
  • So why not adding more `handle_in` functions in the `starting_channel`, if possible, or open and close other channels on demand. Eg. you open the `room_channel` when you are in the chat, you close it when you leave the chat. – iacobSon Oct 09 '17 at 16:18
  • Because I don't know what channels to join yet.. – Shih-Min Lee Oct 09 '17 at 22:46

0 Answers0