Channel layer questions ( would really really appreciate some guidance )

Hi so i need to implement notifications in my application and I have a few questions about channel layer.

  1. Does every consumer instance get its own channel layer name ? ( lets say i have 2 websocket URLs mapped to 2 consumers , and every client establishes a connection to both these consumers via the url router )

  2. Is the channel layer name uniquely generated only for that specific connection ? and therefore might be different if the same consumer spins up another instance of itself for a connection ?

  3. How do i store and access these channel layer names for each user when i need to add them to a group or something . Do i just store them in a database for the duration of the connection and get rid of them after ?

Welcome @SirNiksALot !

Yes. The Channel core needs to know which instance of which consumer to be used as the destination of a channel message.

(I have never found it necessary to do this. I prefer multiplexing one connection per client.)

Yes and Yes.

The Consumer knows what its channel name is. It can add itself to any groups.

If you need to know which users are members of which groups, you would need to manage that yourself. (I use the django-channels-presence package for this purpose.)

1 Like

Oh okay I see .Thank you so much .
I do have one other question though. If I have multiple consumers mapped to multiple routes within the same app . Will they all have different channel names upon instantiation? Or will all of them have the same name ?

Because what If I need to access a channel later from a django view . How do I know which consumer it’s referring to .

Yes.

The easiest way to think about a channel name is that it’s a name for the connection between a consumer and a JavaScript websocket object.

If you have 10 different people each opening up one connection to a consumer, there will be 10 active channel names within your system. If each of those 10 open up a connection to a second consumer, you now have 20 active channel names. (Again, I have never found it necessary to do this, and I would consider it a mistake to architect a system where that would be required. My consumers are minimal - the real work is typically done elsewhere, and the JavaScript on the client dispatches messages appropriately.)

That’s up to you to track. In the simple case, that’s one usage of the Channels “group” facility. In a more complex case, that’s where the Channels Presence package can be useful.