Search code examples
djangodjango-channels

Django channels get the current channel


I want to save messages to my database, but I want to devide them by channels, currently I load messages from other channels from the database, I am wondering how can I get something unique about a certain channel so I could have a field in my model named channel for that?

Edit: To possibly elaborste my question, is there a way I can acces the room instance?


Solution

  • If you enable channel_layers then every time a consumer instance (channel) is created it will automatically instantiate a channel name. This can be accessed from within the consumer with self.channel_name.

    Edit: The channel_name is completely unique to the channel meaning every client that connects will have a unique channel name associated with it.