Where would I store topic id?
As for socket, I can use:
def join("topic:" <> topic_id, _params, socket) do
...
socket= assign(socket, :topic_id, topic_id)
{:ok, socket}
end
That was at socket scope, but my users can join multiple topics at the same time, meaning that the above code will override the topic_id each time a new topic is joined, is that true?
What if I want to know which topic id is active in the handle_in
?
for example:
def handle_in("new_message", params, socket) do
# what is the active topic id here?
end
I though of this:
def handle_in("new_message:" <> topic_id, params, socket) do
# now, I know that topic_id is the active topic
end
Is there another way to do this? or this is how its done?
The join
has a topic so that you can perform additional validation to check the user can subscribe to the topic (check their permissions, etc.)
You are correct, after there is a subscription to the topic, the channels are multiplexed over the socket.
If you wish to pass additional information for a particular message, the params is a common place to put them:
def handle_in("new_message", %{"topic_id" => topic_id}, socket) do
...
end
If you could explain why you need the topic_id then it could help answer your questy,