Skip to content

uploading image files inside consumers.py with Django-Channels #2137

@remoteconn-7891

Description

@remoteconn-7891

I need some help with implementing uploading/receiving image files between end-users with Django-Channels/Websockets inside Consumers.

Here are my consumers.py and also I'm using VueJS as the frontend.

import json
from channels.exceptions import StopConsumer
from channels.generic.websocket import AsyncJsonWebsocketConsumer


# Channel consumer for both chat room group

class ChatConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = f'chat_{self.room_name}'
        
        # Join room group
        await self.channel_layer.group_add(self.room_group_name, self.channel_name)
        
        await self.accept()
        
        
    async def disconnect(self, close_code):

        # Leave room group
        await self.channel_layer.group_discard(self.room_group_name, self.channel_layer)
        
    # Receive message from WebSocket
    
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        
        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name, {'type': 'chat.message', 'message': message}
        )
        
    # Receive message from room group
    
    async def re(self, event):
        message = event['message']
        
        # send message to websocket
        await self.send(text_data=json.dumps({'message': message}))
        
        
# Channel Consumer that consumed events for uploading/receiving image files in the message room

class UploadImageConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        await self.accept()
        

    async def disconnect(self, close_code):
        raise StopConsumer()
    
    
    async def receive(self, bytes_data=None, text_data=None):
        if bytes_data:          
            # Handles image file chunks (binary data)
            
            with open('uploaded_image.jgp', 'ab') as f:
                f.write(bytes_data)
            await self.send(text_data=json.dumps({'message': 'Image chunk received'}))
        elif text_data:
            # Handles metadata (text data)
            
            await self.sesnd(text_data=json.dumps({'message': text_data}))
            
            
    async def send_image(self, event):
        image_data = event['image_text']
        await self.send(bytes_data=image_data)

Sorry I'm still new to Django-Channels/Websockets. The files uploaded (images) don't need to be large in bytes. Please let me know if you to see additional relevant files in my Django project

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions