Broadcast msg to all users in common topic

Hi All,
How could I send msg to all users in single “room” topic?
Users have uniq subtopics, but common topic -> “room:#{uniq_id}”

I’ve tried that ways, but these send msg only to one user.

1.broadcast(socket, "online_users", payload)
2.ChatWeb.Endpoint.broadcast!("room:*", "online_users", payload)
3.push(socket, "online_users", payload)

for now I added another subtopic to handle general messages, and it works

#app.js
 let general = socket.channel("room:general");
 let channel = socket.channel(`room:${socketParams.token}`, { name: name });

#room_channel.ex
ChatWeb.Endpoint.broadcast!("room:general", "online_users", payload)

but I am wondering if there is better way to do it

You can create many different channels, for different purpose, not only room.

I use lobby for general purpose messages, or system…

And a user channel, by id for reaching them individually.

It’s very cheap to create a channel, and it’s using the same socket.

1 Like