I am setting up Websockets using Django Channels
, sending a test message when the relevant API end point is triggered. However when I try to use Postman to test my websocket connection, I successfully connect, receive the JSON I am sending and immediately get disconnected. This is the full traceback:
redis.exceptions.ConnectionError: Error 10061 connecting to localhost:49155. No connection could be made because the target machine actively refused it.
HTTP GET /media/profile/2022/06/10/photo-1615672968435-95ade28e0b38.jpg 500 [4.47, 127.0.0.1:51105]
HTTP GET /users/1/ 500 [4.46, 127.0.0.1:51106]
WebSocket HANDSHAKING /stories/notification_testing/ [127.0.0.1:50570]
{'type': 'websocket', 'path': '/stories/notification_testing/', 'raw_path': b'/stories/notification_testing/', 'headers': [(b'sec-websocket-version', b'13'
), (b'sec-websocket-key', b'TcKbMvNYlHQtJdO5efSXDQ=='), (b'connection', b'Upgrade'), (b'upgrade', b'websocket'), (b'sec-websocket-extensions', b'permessage
-deflate; client_max_window_bits'), (b'host', b'127.0.0.1:8000')], 'query_string': b'', 'client': ['127.0.0.1', 50570], 'server': ['127.0.0.1', 8000], 'sub
protocols': [], 'asgi': {'version': '3.0'}, 'cookies': {}, 'session': <django.utils.functional.LazyObject object at 0x00000207DC59A5E0>, 'user': <channels.auth.UserLazyObject object at 0x00000207D8D5A820>, 'path_remaining': '', 'url_route': {'args': (), 'kwargs': {}}}
WebSocket CONNECT /stories/notification_testing/ [127.0.0.1:50570]
Exception inside application: [Errno 11003] getaddrinfo failed
Traceback (most recent call last):
...
File "C:\Users\15512\Desktop\django-project\peerplatform\signup\consumers.py", line 31, in websocket_connect
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
...
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11003] getaddrinfo failed
WebSocket DISCONNECT /stories/notification_testing/ [127.0.0.1:50570]
I am running Redis through a Django image, I am already using a Redis cache for different purposes. This is how I set up my Channels layer in Settings.py
:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("default:redispw@127.0.0.1", 49155)],
},
},
}
This is the location of my Redis instance from Docker
"LOCATION": "redis://default:redispw@localhost:49153",
I set up the router for my websockets in asgi.py
:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'signup.settings')
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
[path('stories/notification_testing/', NotificationConsumer.as_asgi())]
))
})
I noticed in the trackback, one of the errors seems to refer to line 31 in my consumers.py file, here's the specific function from consumers.py:
class NotificationConsumer(AsyncWebsocketConsumer):
async def websocket_connect(self, event):
print(self.scope)
await self.accept()
await self.send(json.dumps({
"type": "websocket.send",
"text": "hello world"
}))
self.room_name='test_consumer'
self.room_group_name='test_consumer_group'
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
self.send({
"type": "websocket.send",
"text": "room made"
})
Just in case this is relevant too, here is my models.py:
class Notifications(models.Model):
sender = models.ForeignKey(User, null=True, blank=True,
related_name='sender', on_delete=models.CASCADE)
receiver = models.ForeignKey(User, null=True, blank=True,
related_name='receiver', on_delete=models.CASCADE)
status = models.CharField(max_length=264, null=True, blank=True,
default="unread")
type_of_notification = models.CharField(max_length=264, null=True, blank=True)
What did I try?
redis://default:redispw@localhost, 49153
and received the same errordjango-channels
& redis-channels
Django-Channels fails while giving name or service unknown error
I had same issue. Redis URL was incorrect. After I changed it, it worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.