I’m trying to send data to a websocket from outside of the consumer
so i did following:
settings.py
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
routing.py
from django.urls import path
from .consumers import CoinsListConsumer
websocket_urlpatterns = [
path('ws/coins/', CoinsListConsumer.as_asgi())
]
asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from apps.coins.routing import websocket_urlpatterns
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns,
)
)
)
})
consumers.py
class CoinsListConsumer(AsyncWebsocketConsumer):
async def connect(self):
logger.info('Websocket was connected.')
await self.accept()
async def disconnect(self, code):
logger.warning('Websocket was disconnected.')
pass
async def receive(self, text_data=None, bytes_data=None):
return await super().receive(text_data, bytes_data)
well this is ok and when i go to a view… the websocket will connect very well
but when i want to send data to the websocket.
def send_data_to_websocket_coins_list_view(data: List[Dict]) -> None:
"""Send data to websocket coins list view """
async_to_sync(channel_layer.send)(json.dumps(data))
This did not work and raised following error
TypeError: send() missing 1 required positional argument: 'message'
Also in the documentation this is should work by following code
async_to_sync(channel_layer.send)("channel_name", json.dumps({...}))
its also not worked and raise the following error
AssertionError: message is not a dict
So what is the problem? what should i do?
2
Answers
You don’t need to dump data
You can use it in an ApiView for example: