1

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?

simo
  • 23,342
  • 38
  • 121
  • 218

2 Answers2

6

Users can join multiple topics. You can access the current topic from the socket param in handle_in.

So assuming the room topic's base is "topic", here's how you can get the topic id:

def handle_in("some_message", _params, socket) do
  "topic:" <> topic_id = socket.topic
  ...
end
Alex Garibay
  • 391
  • 1
  • 5
0

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,

Gazler
  • 83,029
  • 18
  • 279
  • 245
  • Thank you, I need the topic ID, as there might be topic related settings when messages are sent.. also, I thought that socket object is at channel scope, but then I found that its at user scope, so I wanted to make sure that I got it right, can the params inside socket object be encrypted? – simo May 19 '16 at 15:04
  • Actually, users can join territories channels, territory ID will be used to read associated polygon, then I can check if sent lat, long message inside or outside the territory's polygon.. that's an example of why would I need topic ID.. – simo May 19 '16 at 16:24
  • Send territory_id in the params of you need it. – Gazler May 19 '16 at 16:25