Tuesday, April 25, 2017

Multiplexing results from multiple models in socket

Hi,

I have setup Django channels to fetch `messages` from a given `room`. However, I would like to change this logic to receive messages from several rooms. In DRF, I use serializers for this. I saw some for loops doing this, but I believe it is very inefficient.

What is the correct way to do this with a socket?

Also, I'd like the initial `ws_connect` to of course fetch any static result that exist already (currently this works, but only for a single `room`).

Ideally, my result would come like this:

# Desired result

[
   
{
       
"label": "Foo",
       
"pk": 1,
       
"message": [
           
{
               
"handle": "Bruce",
               
"message": 1
           
},
           
{
               
"handle": "Sol",
               
"message": 2
           
}
       
]
   
},
   
{
       
"label": "Bar",
       
"pk": 2,
       
"message": [
           
{
               
"handle": "Sol",
               
"message": 1
           
},
           
{
               
"handle": "Alfred",
               
"message": 2
           
}
       
]
   
}
]


FYI, here are my connect & receive models

# consumers.py

def ws_connect(message):
    prefix
, label = message['path'].strip('/').split('/')
   
# temporarily just getting the room from URL
    room
= Room.objects.get(label=label)
   
# multiroom = Room.objects.all()
    chathistory
= room.messages.all().order_by('timestamp')
   
Group('chat-' + label).add(message.reply_channel)
    message
.channel_session['room'] = room.label
    message
.reply_channel.send({'text': json.dumps([msg.as_dict() for msg in chathistory.all()])})


@channel_session
def ws_receive(message):
    label
= message.channel_session['room']
    room
= Room.objects.get(label=label)
    data
= json.loads(message['text'])
    m
= room.messages.create(handle=data['handle'], message=data['message'])
   
Group('chat-'+label).send({'text': json.dumps([m.as_dict()])})




# models.py


class Room(models.Model):
    name
= models.TextField()
    label
= models.SlugField(unique=True)

class Message(models.Model):
    room
= models.ForeignKey(Room, related_name='messages')
    handle
= models.TextField()
    message
= models.TextField()
    timestamp
= models.DateTimeField(default=timezone.now, db_index=True)
   
def __unicode__(self):
       
return '[{timestamp}] {handle}: {message}'.format(**self.as_dict())


   
@property
   
def formatted_timestamp(self):
       
return self.timestamp.strftime("%H:%M:%S")
   
   
def as_dict(self):
       
return {'pk':self.pk, 'handle': self.handle, 'message': self.message, 'timestamp': self.formatted_timestamp}




Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ab372b04-2507-4a18-8e31-acb0c6691b09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment