Channels (topics/subtopics) conventions (naming, code structure)

(Note: hypothetical code examples below are in context of example chatting app)
(Note: I’m also beginner, so I apologize for any misunderstanding in the upcoming text)

Hello,
I’d like to ask how should I construct my channels.
Now I’m standing on the beginning and I dont know how to start… Most examples show how to create channel with topic: room:room_id. Thats ok, I understand that. But what about events such as room_created, room_renamed, room_deleted. Is it OK to write a channel with dedicated topic for such events? Also, where should I put code for broadcasting such events? Would it be inside the responsible socket’s handle_in("create_room" ...) (the one that originally issued a request for creating new room)? Or should I have some sort of PubSub baked into my business logic code that would accept that “new room” has been created and forward this information to interested sockets/channels? (if so, would it be a dedicated channel for this?)

Thanks in advance

Hello!

You can create a lobby room where you’ll dispatch general messages such as room_created… so basically, when user join your chat app, they’ll join the room:lobby channel. After that their free to join other channel like `room:{id}ˋ

Where lobby is a fixed value and id is a variable.

I hope it help you :wink:

Thanks! That clarifies things.

And where would you notify other users about newly created room?

When user join a room (e.g the lobby room) you can broadcast message into that room. So every user in lobby will be notify that a new room was created.

Take a look here Channels — Phoenix v1.5.7

In the handle_in function there is a broadcast message to socket (so in the channel)

Edit, a better link Phoenix.Channel — Phoenix v1.5.7

It explains that you can use the endpoint module to broadcast in other channel.

Oh yes, the docs indeed solve the problem I’m talking about… Has not realized that. It also makes sense to keep websocket stuff and event distribution inside the channel… (or the web app in general).

Thanks for the help :slight_smile:

1 Like