How to properly set up django channels and websocket

I’m trying to implement websocket and django channels. On frontend I have react and on backend django.

I’ve installed channels and added it to INSTALLED_APPS in settings and I also added to settings.py

ASGI_APPLICATION = 'VideoScreen.routing.application'
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels.layers.InMemoryChannelLayer',
    },
}

The routing is as follows:

websocket_urlPattern = [
    path("ws/device-key/", DeviceCodeConsumer.as_asgi()),
]
application = ProtocolTypeRouter(
    {
        "http": get_asgi_application(),
        "websocket": AuthMiddlewareStack(URLRouter(websocket_urlPattern)),
    }
)

channel_routing = {"http.request": StaticFilesHandler()}

And DeviceCodeConsumer is as follows:

class DeviceCodeConsumer(WebsocketConsumer):
    def connect(self):
        self.group_name = 'device_key'

        async_to_sync(self.channel_layer.group_add)(
            self.group_name,
            self.channel_name
        )

        print(f"Added to group {self.group_name}")  # Debugging message

        # Accept the WebSocket connection
        self.accept()

    def receive(self, text_data):
        # Handle incoming JSON messages if needed
        print('RECIEVED: ' + text_data)
        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        async_to_sync(self.channel_layer.group_send)(
            self.group_name,
            {
                'type': 'update_device_key',
                'message': message
            }
        )

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

        self.send(text_data=json.dumps({
            'type': 'update_device_key',
            'message': message
        }))

Thus I create group with name device_key and I want to add a message to that group when some code executes in django view and to notify the frontend about it. The view in django that I have is as follows:

@login_required
def device_edit_view(request, device_id):
    if device_id <= 0:
        device = Device()
    else:
        device = Device.objects.get(pk=device_id)

    if request.method == "POST":
        print("POST method reached")  # Debugging message to confirm POST method
        form = DeviceEditForm(data=request.POST, instance=device)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            device_id = instance.id
            messages.success(request, "Your device has been saved")

            # Notify the WebSocket group using the utility function
            notify_websocket_group(
                "device_key",  # Group name
                "update_device_key",  # Type of the message
                instance.key
            )
            return redirect("/devices")
    else:
        form = DeviceEditForm(instance=device)
    form.fields["playlist"].queryset = Playlist.objects.filter(
        company_id=request.session[COMPANY_ID]
    )
    context = {
        "form": form,
        "device": device,
    }

    return render(request, "play/device-edit.html", context)

Where i have # Notify the WebSocket group using the utility function I want to add the channel group that there is a change and I should also get a notification on frontend. notify_websocket_group is as follows:

def notify_websocket_group(group_name, event_type, message):
    """
    Notifies a WebSocket group with a given message.

    Args:
        group_name (str): The group to send the message to.
        event_type (str): The type of the message (for example, 'device_key').
        message (str): The message content.
    """

    print(f"Notifying group: {group_name}, Event Type: {event_type}, Message: {message}")  # Debugging message

    channel_layer = get_channel_layer()
    async_to_sync(channel_layer.group_send(
        group_name,
        {
            "type": event_type,  # The method to call on the consumer
            "message": message,
        }
    ))

    print('Sent WebSocket message to group device_key')  # Debugging message

I see all those print in terminal, but that should also call recieve from DeviceCodeConsumer and I should see print(‘RECIEVED: ‘ + text_data). But I do not see that.

What am I doing wrong?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật