Answer the question
In order to leave comments, you need to log in
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)
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 questionAsk a Question
731 491 924 answers to any question