My websocket works, but doesn't send any data to the group
# settings.py
INSTALLED_APPS = [
'daphne',
# admin interface
'jet',
# django apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 3rd party
'django_otp',
'django_otp.plugins.otp_totp',
'drf_spectacular',
'rest_framework',
'multiselectfield',
'django_celery_results',
'django_celery_beat',
'channels',
# my apps
"ads",
"authentication",
]
# WSGI_APPLICATION = 'gozle_ads.wsgi.application'
ASGI_APPLICATION = 'gozle_ads.asgi.application'
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost", 6379)],
},
},
}
# consumers.py
import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
class BannerConsumer(WebsocketConsumer):
def connect(self):
self.accept()
self.room_name = "banner_changer"
self.room_group_name = "banner_changer_group"
async_to_sync(self.channel_layer.group_add)(
self.room_name, self.room_group_name
)
self.send(json.dumps({"messgae": "Connected to the banner websocket"}))
def receive(self, text_data=None):
async_to_sync(self.channel_layer.group_send)(
"banner_changer_group",
{
"type": "banner_ads_socket",
"value": f"{text_data}"
}
)
def disconnect(self, code):
print("DISCONNECTED")
def banner_ads_socket(self, event):
print("BANNER ADS")
print(event)
# asgi.py
import os
from django.core.asgi import get_asgi_application
from django.urls import re_path
from channels.routing import ProtocolTypeRouter, URLRouter
from ads.consumers import BannerConsumer
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gozle_ads.settings')
ws_patterns = [
re_path(r"ws/banner/", BannerConsumer.as_asgi())
]
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": URLRouter(ws_patterns),
})
when I'm sending data to the receive
method nothing is happening, method banner_ads_socket
doesn't work. It doesn't raise any errors. First, I'm adding consumer to my group, then sending data to the group. I copied that from video. In the this code works, but my code not. What can I do with that?