send values from views.py to consumers.py

consumers.py

from channels.generic.websocket import AsyncWebsocketConsumer
import json

class MyConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_group_name = 'mygroup'

        # 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_name
        )

    # Receive message from WebSocket
    async def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        print(message)

        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'group_message',
                'message': message
            }
        )

    # Receive message from room group
    async def group_message(self, event):
        print(event, "----> event")
        message = event['message']
        print(message)

        # Send message to WebSocket
        await self.send(text_data=json.dumps({
            'message': message
        }))
    
    async def get_view_value(self, value):
        print("get-view-called")
        await self.channel_layer.group_send(
            self.room_group_name,
            {
                'type': 'kafka_message',
                'message': value
            }
        )

views.py

from django.shortcuts import render
from django.http import JsonResponse
from django.http import HttpResponse

from .consumers import MyConsumer
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync


class GetValuesFromAM(object):
    # def __init__(self):
    #     self.session_list = []
    session_list = []
    transaction_values_list = []
    consumer_instance = MyConsumer()
    
    async def sessionValues(self, request):
        print("Getting Session Vals!!")
        try:
            customer = request.GET.get("Customer")
            session_id = request.GET.get("SessionId")
            

            print(f"Customer: {customer}")
            print(f"SessionID: {session_id}")
            
            session_dict = {
            "data": {
                "Customer": customer, 
                "SessionId": session_id, 
                }
            }

            self.session_list.append(session_dict)

            channel_layer = get_channel_layer()
            async_to_sync(channel_layer.group_send)(
                'mygroup',
                {
                    'type': 'group.message',
                    'message': self.session_list
                }
            )

            return HttpResponse("Updated Session Values!!")
        except Exception as e:
            return HttpResponse(f"Error: {str(e)}")

routing.py

from django.urls import path
from .consumers import MyConsumer

websocket_urlpatterns = [
    path('ws/myapp/', MyConsumer.as_asgi()),
]

I’m using react for front-end.

i need help with sending the session list value from views to consumers to frontend using websockets

Please describe the problem that you’re having. Include all symptoms that you are seeing, especially any error messages that may be showing up in either the server or the browser.

Hi, I have resolved the issue.
thank you