Y
Y
Yeldos Adetbekov2017-07-13 23:11:34
Django
Yeldos Adetbekov, 2017-07-13 23:11:34

Which variant of django real-time is better?

Good day, comrades! I recently encountered django-channels here. I wanted to write a user2user chat, with the ability to expand (user2users or groupchat). I thought of only 3 options:
1) Create separate models
models.py

class Message(models.Model):
    user_from = models.ForeignKey(User, related_name="user_from")
    user_to = models.ForeignKey(User, related_name="user_to")
    created = models.DateTimeField(default=datetime.now())
    message = models.TextField(blank=False)
    delivered = models.DateTimeField(null=True, blank=True)
    viewed = models.DateTimeField(null=True, blank=True)
    deleted_out = models.DateTimeField(null=True, blank=True)
    deleted_in = models.DateTimeField(null=True, blank=True) 
    size = models.FloatField(default=1.0)
class GroupMessage(models.Model):
    group = models.ForeignKey(Group, related_name="groupchat")
    user_from = models.ForeignKey(User, related_name="user_from")
    user_to = models.ForeignKey(User, related_name="user_to")
    created = models.DateTimeField(default=datetime.now())
    message = models.TextField(blank=False)
    delivered = models.DateTimeField(null=True, blank=True)
    viewed = models.DateTimeField(null=True, blank=True)
    deleted_out = models.DateTimeField(null=True, blank=True)
    deleted_in = models.DateTimeField(null=True, blank=True) 
    size = models.FloatField(default=1.0)

Connect via websocket to a channel, create a common Group("chat"). Check if the user is communicating with another user, then return the serialized data. The client has to listen to this group and render data directly from the socket. But here the problem is that a mess is created in one single group, and with each change in the state, you have to do extra checks.
2) All the same, but in some clever way create a Group for specific users. I don’t know how to take it to generate a unique hash from usernames of users. And force specific users to listen to this channel. But in this case, there is a risk of data leakage.
3) Create a single Message model, in which to leave the attribute Room "null" (I have a dislike for nulls), Create a Group () based on this Room. But how then to distinguish a group from a simple dialogue? Add another type field to Room? And how to implement channels correctly? Please help))
Oh yes, here is the consumers.py code with the 'chat' Group. Everything works, only it turned out to be a general chat, so far without saving, later after the normal architecture, I will create Message instances
class UserSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    username = serializers.CharField(max_length=100)
    first_name = serializers.CharField(max_length=100)
    last_name = serializers.CharField(max_length=100)

class Payload(object):
    def __init__(self, j):
        self.__dict__ = json.loads(j)

def ws_connect(message):
    message.reply_channel.send({"accept": True})
    Group('chat').add(message.reply_channel)

def ws_message(message):
    token = jwt_response_payload_handler(Payload(message.content["text"]).token)
    user = VerifyJSONWebTokenSerializer().validate(token)["user"]
    Group('chat').send({'text': json.dumps({'message': Payload(message.content["text"]).message,
                                            'sender': UserSerializer(user).data})})

def ws_disconnect(message):
    Group('chat').discard(message.reply_channel)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question