Django Channels websocket consumers

Hello, I’m working on a Django chat with a flutter front end and I’m unsure how to proceed, i figured the models and the relations i want between them but I’m unable to comprehend consumers despite my best efforts, i understand the basics of connecting, disconnecting and receiving but I’m unsure how to bend them to suit my needs.My intended workflow is that a user after logging in selects conversation from the list, and starts sending/receiving messages in it. In case the user has no conversation he can select a user from another list and create a conversation with him.
this is my models .py :

class Conversation(models.Model):
    name = models.CharField(max_length=256)
    last_message = models.CharField(max_length=1024, null=True)
    last_sent_user = models.ForeignKey(User, on_delete=models.PROTECT, null=True)
    def __str__(self):
        return self.name

  
class Message(models.Model):
    sender = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='sent_messages')
    conversation = models.ForeignKey(Conversation, on_delete=models.CASCADE, related_name='messages')
    timestamp = models.DateTimeField(default=timezone.now)
    content = models.TextField(blank=True)
    image = models.ImageField(upload_to='message_images/', blank=True, null=True)
    def displaymessage(self):
        if self.image:
            return (self.image.url)
        else:
            return self.content
        
class UserInbox(models.Model):
    channel = models.CharField(max_length=256)
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    def __str__(self):
        return str(self.channel)



class GroupMembers(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    conversation = models.ForeignKey(Conversation, on_delete=models.PROTECT)
    joined_at = models.DateTimeField(auto_now_add=True)
    left_at = models.DateTimeField(blank=True, null=True)

I’m using the default channel “InMemoryChannelLayer” as I’m building the app for demonstration on physical phone as part of a final year project and not for deployment.
I’m unsure of many things such as: Primarily what arguments should i pass to my websocket_connect, and how to send the messages to the specific channel I’m accessing,My short term goal is to test a successful conversation creation with another user. login with the different user and see the conversation/message.
I’ve dedicated a couple of weeks to figure this out and I’m stumped for ideas, I’d appreciate any pointers or if you can recommend any tutorials or books on the matter. I’m sorry for my blathering and thanks for any input.

If you’re worked your way through the official [Channels tutorial](
(Tutorial Part 1: Basic Setup — Channels 4.0.0 documentation), then I suggest you go back through it and read the code in its entirety, to try and re-solidify the concepts it presents. (It is, after all, a tutorial on a Chat system, making it specifically related to what you’re trying to do.)

(If you haven’t worked your way through it, now’s the time to do so.)

There are also a number of threads here containing more detailed information:

See:

as a couple of recent examples.

I had also found Andrew Godwin’s multichat in channels-examples to have been very helpful to me when I was getting started with channels. I don’t think it’s specifically valid any more - it was written for Channels 1 - but the fundamental concepts presented would still apply.

This really isn’t a good idea if you’re going to demo this with connections from multiple systems. See the warning at Channel Layers — Channels 4.0.0 documentation

1 Like

Thanks for the swift response, I’ll make sure to read all the referenced material and hopefully formulate a fitting ChatConsumer.

Should i use the redis feature instead then? i failed to mention that i’m on windows and am using MySQL as my database and unsure if that’s relevant

If at all possible, I’d try really hard to set up a Linux environment for this. Both VirtualBox and WSL work really well for giving you a suitable system.

(The database doesn’t matter so much. The bigger issue is that Redis is not officially supported on Windows.)

1 Like

WSL can be used to “host” redis quite easily.
Only shame is redis requiring a whole WSL…

for what it’s worth, I usually go with a virtualbox and some port forwarding so that I can interact (from the host) with whatever is running (in the guest). Be it over http, ssh, mysql … or redis.
It can be a bit time consuming to set up, but it usually runs great, and offers a true … linux work env. So less surprises when push-top-prod needs to happen :slight_smile:

1 Like

Can you please explain what using Linux is required for , my end goal is showing two instances of the flutter app confirming the chatting feature works and a possible audio/video call implementation.

ho if you do not need linux that’s fine.
Only… if you plan on having your product/site running and being available to the outside world, linux is “generally” the way to go.
T’was just my 2pence :slight_smile:

Thanks, i won’t be needing that as i said the app is entirely for demonstration purposes on my physical phone and not to be uploaded to any platform or anything at this stage. with that said is redis still a requirement ?

Actually, I’ll be impressed if you can get that configuration to work at all. Good luck!

if it is hard to get it work i’ll follow your method of redis, however this is a pure django flutter connection and am unsure of the role of virtualbox and linux in it .