'while adding a group in django channels layer using rabbitmq server? rabbitmq server not connecting

I am using django-channels with rabbitmq server. Installed packages:

channels==2.4.0
channels-rabbitmq==1.2.1
rabbitmq_server-3.5.7

consumer.py:

import asyncio
import json
from django.contrib.auth import get_user_model
from channels.consumer import AsyncConsumer
from channels.db import database_sync_to_async
from asgiref.sync import async_to_sync
class NotificationConsumer(AsyncConsumer):
    async def websocket_connect(self, event):
        print("connected", event)
        await self.send({
        "type": "websocket.accept"
        })
        user = self.scope['user']
        await self.send({
            "type": "websocket.send",
            "text": "Hello World"
        })
        print(self.channel_name)
        async_to_sync(self.channel_layer.group_add("gossip", self.channel_name))
        print(f"Added {self.channel_name} channel to gossip")

    async def websocket_received(self, event):
        print("receive", event)

    async def websocket_disconnect(self, event):
        print("disconnected", event) 
        await self.channel_layer.group_discard("gossip", self.channel_name)
        print(f"Removed {self.channel_name} channel to gossip")

routing.py:

from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from django.urls import path
from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator
from problems.consumer import NotificationConsumer
import problems.routing

application = ProtocolTypeRouter({
'websocket':AllowedHostsOriginValidator(
   AuthMiddlewareStack(
      URLRouter(
          [
              path('', NotificationConsumer),
          ]  
        )
      )
   )
})

Channels setting in settings.py:

`ASGI_APPLICATION = 'codeyoda.routing.application'

CHANNEL_LAYERS = {
     "default": {
         "BACKEND": "channels_rabbitmq.core.RabbitmqChannelLayer",
         "CONFIG": {
             "host": "amqp://guest:[email protected]/asgi",
          },
      },
  }

Error:

(codeyodaEnv) vagrant@ubuntu-xenial:/vagrant/codeyoda-project$ python manage.py runserver 0.0.0.0:8080
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 21, 2020 - 12:07:56
Django version 2.2.4, using settings 'codeyoda.settings'
Starting ASGI/Channels version 2.4.0 development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.
HTTP GET / 200 [0.24, 10.0.2.2:62960]
HTTP GET /static/css/base.css 304 [0.05, 10.0.2.2:62960]
HTTP GET /static/css/home.css 304 [0.04, 10.0.2.2:62961]
HTTP GET /static/css/tech-skills.css 304 [0.04, 10.0.2.2:62962]
HTTP GET /static/js/base.js 304 [0.05, 10.0.2.2:62963]
HTTP GET /static/images/logo.png 304 [0.03, 10.0.2.2:62963]
HTTP GET /uploads/static/images/PYTHON/python_JxDdwwZ.png 304 [0.05, 10.0.2.2:62962]
Not Found: /uploads/static/images/JAVASCRIPT/interview.png
HTTP GET /uploads/static/images/JAVASCRIPT/interview.png 404 [0.07, 10.0.2.2:62963]
Not Found: /uploads/static/images/hiring/basic_programming.jpg
HTTP GET /uploads/static/images/hiring/basic_programming.jpg 404 [0.08, 10.0.2.2:62961]
Not Found: /uploads/static/images/das/basic_programming.jpg
WebSocket HANDSHAKING / [10.0.2.2:62964]
HTTP GET /uploads/static/images/das/basic_programming.jpg 404 [0.11, 10.0.2.2:62960]
connected {'type': 'websocket.connect'}
WebSocket CONNECT / [10.0.2.2:62964]
admin
channels_LVrBroNbZmuB!rpQFvCasTpba
Added channels_LVrBroNbZmuB!rpQFvCasTpba channel to gossip
only PLAIN login_method is supported, falling back to AMQPLAIN
HTTP GET /static/images/favicon.ico 200 [0.02, 10.0.2.2:62960]
Connection lost exc=ConnectionResetError(104, 'Connection reset by peer')
Connect/run on RabbitMQ failed: AmqpClosedConnection(); will retry in 1.000000s
only PLAIN login_method is supported, falling back to AMQPLAIN
Connection lost exc=ConnectionResetError(104, 'Connection reset by peer')
Connect/run on RabbitMQ failed: AmqpClosedConnection(); will retry in 1.000000s
only PLAIN login_method is supported, falling back to AMQPLAIN
Connection lost exc=ConnectionResetError(104, 'Connection reset by peer')

Followed tutorial here. I am able to connect with WebSocket properly and receiving and sending data as well. But once I defined channel_layer settings it is connecting to WebSocket but not able to connect with rabbitmq server I think the problem is in the host definition. I verified that there is a user by username guest but still not able to connect



Solution 1:[1]

Please make sure your rabbitMQ vhost run as /asgi that you mentioned in settings file like

CHANNEL_LAYERS = {
     "default": {
         "BACKEND": "channels_rabbitmq.core.RabbitmqChannelLayer",
         "CONFIG": {
             "host": "amqp://guest:[email protected]/asgi",
          },
      },
  }

Default rabbitMQ VHOST is / change your default / to /asgi from rabbitmq conf file after that execute your django runserver.

Other solution

Change on this

CHANNEL_LAYERS = {
     "default": {
         "BACKEND": "channels_rabbitmq.core.RabbitmqChannelLayer",
         "CONFIG": {
             "host": "amqp://guest:[email protected]//",
          },
      },
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1