hagsteel / swampdragon

swampdragon
Other
556 stars 73 forks source link

Private chat implementation #214

Closed ghost closed 7 years ago

ghost commented 7 years ago

How to send data to a specific user (send data to reciever privately)? my code is

class PrivateMessagesRouter(ModelPubRouter):
    route_name = 'swampdragon-private-messages'
    valid_verbs = ['subscribe', 'unsubscribe',  'create']
    model = Message
    serializer_class = PrivateMessageSerializer
    paginate_by = 10

    def get_subscription_channels(self, **kwargs):
        print("subscribed channels"),
        print(kwargs)
        return ['sd_private_messages']

    def create(self, **kwargs):
        user = self.connection.user
        if user:
            print("private message creating")
            kwargs.update({"user": user})
            message = Message.objects.create(**kwargs)
            receiver = message.receiver
            serializer = self.serializer_class(instance=message, user=user)
            data = serializer.serialize()
           # send data to reciever privately
            self.publish(self.get_subscription_channels(), data)