dakota
dakota

Reputation: 75

Django Channels __init__() got an unexpected keyword argument 'scope'

I have been trying to set up a basic Django Channels project based on the tutorial, but I am getting this error. I used the Django python shell to test if Redis is working and it is. I am running Daphne with 'Daphne my_project.asgi:application. I could not find any similar questions or documentation on the problem. Please let me know if you need to see any other parts of my code. Any help is greatly appreciated!

consumers.py

from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
import json

class TestConsumer(WebsocketConsumer):

def connect(self):
    self.group_name = 'test'

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

    self.accept()

def disconnect(self, close_code):
    async_to_sync(self.channel_layer.group_discard)(
        self.group_name,
        self.channel_name
    )

def receive(self, 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': 'chat_message',
            'message': message
        }
    )

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

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


project routing.py

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import main.routing
import sockets.routing

application = ProtocolTypeRouter
({
    'websocket' : AuthMiddlewareStack(
        URLRouter(
            sockets.routing.websocket_urlpatterns
        )
    )
})


asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'socialapp.settings')
django.setup()
application = get_default_application()


project settings

ASGI_APPLICATION = 'socialapp.routing.application'

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
    },
}

Upvotes: 3

Views: 1927

Answers (2)

Miniature Judge
Miniature Judge

Reputation: 21

For anyone looking this up in the future, this issue is because of not running the development server. Make sure you have your django development server(python manage.py runserver) and redis both are up and running.

Upvotes: 0

Harrison
Harrison

Reputation: 186

Try double checking you logic as the files look fine: when i saw this, i had erroneously passed my uwsgi.application to daphne instead of the asgi.application

Upvotes: 1

Related Questions