简体   繁体   中英

How can I send real-time data using Django WebSockets?

I would like to send real-time data from external API using Django WebSockets. My view with HTTP looks in this way:

@permission_classes([GetPermission])
class DataList(GenericAPIView):

    serializer_class = ObjectSerializer

    def get(self, request):
        parameter = self.request.query_params.get('parameter', None)
        queryset = ExternalAPI().get(parameter, "RSQA")
        id = Object.objects.get(parameter=parameter).id
        queryset["id"] = id
        return Response(queryset)

I would like use WebSockets instead of HTTP to send my data constantly. Is it a good solution and acceptable to send it without request? I wonder how it should be done? I will be grateful if example would be shown on my GenericAPIView .

Django is synchronous by nature and hence by itself it was/is not possible to achieve web sockets. However one of the core developers of django(I believe its Andrew Goodwin, correct me if its wrong!) developed channels for this purpose. Channels although was supposed to be part of django, now its a django project but seperate from the framework. I have tried channels once long back when it just came out. Its fine little thing, though you should know its philosophy as I quote.

Channels is deliberately designed to prefer low latency (goal is a few milliseconds) and high throughput over guaranteed delivery, which doesn't match some message queue designs.

Some features, like guaranteed ordering of messages, are opt-in as they incur a performance hit, but make it more message queue like.

If this does not sound like what you want, go for something like django-websocket-redis .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM