arriff
arriff

Reputation: 439

How to run Django channels with StreamingHttpResponse in ASGI

I have a simple app that streams images using open cv and the server set in wsgi. But whenever I introduce Django channels to the picture and change from WSGI to ASGI the streaming stops. How can I stream images from cv2 and in the same time use Django channels? Thanks you in advance

My code for streaming:

def camera_feed(request):
    stream = CameraStream()
    frames = stream.get_frames()
    return StreamingHttpResponse(frames, content_type='multipart/x-mixed-replace; boundary=frame')

settings.py:

ASGI_APPLICATION = 'photon.asgi.application'

asgi.py

application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': AuthMiddlewareStack(URLRouter(ws_urlpatterns))
})

Upvotes: 4

Views: 1200

Answers (1)

techbipin
techbipin

Reputation: 82

First, we don't need StramingHTTPResponse for sending image data at all ...

For this, first, ensure you have a Django version with 3.x and Python 3.7+.

Then, install django-channels third party package.

Configure the ASGI application as follows:

import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import .routing

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
        URLRouter(
            app.routing.websocket_urlpatterns
        )
    )
})

Then You need to set ASGI_APPLICATION constant in the settings.py file:

ASGI_APPLICATION = "myproject.asgi.application"

After that, just create an async WebSocket consumer in the consumers.py file present in the application :

import json
from channels.generic.websocket import AsyncWebsocketConsumer


class PairingChat(AsyncWebsocketConsumer):

    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        await self.channel_layer.group_add(
            self.room_group_name,
            self.channel_name
        )
    
        await self.accept()

    async def disconnect(self):

        await self.channel_layer.group_discard(
            self.room_group_name,
            self.channel_name
        )


    # Asyncwebsocket consumer can send any type of data ...

    async def receive(self, text_data):
        data_json = json.loads(your_data)
        message = data_json['message']

        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': '# send your data from here ...',
                'message': message,
                'user': self.scope['session']['name']
            }
        )


    async def chat_message(self, event):
        message = event['message']

        await self.send(data=json.dumps({
            'user': event['user'],
            'message': message,
        }))

Create a route for asyncwebsocket consumers as well ...

from django.urls import re_path
from . import consumers

websocket_urlpatterns = [
    re_path(r'ws/chat1/(?P<room_name>\w+)/$', consumers.PairingChat.as_asgi()),
]

Then, just create a WebSocket client in javascript ... and you are good to go ...

Link for JS Websocket Create : javascript-websocket

Upvotes: 1

Related Questions