pythondjangowebsocketdjango-channelsdaphne

AttributeError in ASGI/Daphne Django problem


I had done this tutorial for WebSocket with Django but I have this problem when I execute "python manage.py runserver":

HTTP GET /chat/hello/ 200 [0.01, 127.0.0.1:65009]
WebSocket HANDSHAKING /ws/chat/hello/ [127.0.0.1:65014]
Exception inside application: 'int' object has no attribute 'decode'
Traceback (most recent call last):
  File "D:\Projects\New Backend\venv\lib\site-packages\django\contrib\staticfiles\handlers.py", line 101, in __call__
    return await self.application(scope, receive, send)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\routing.py", line 62, in __call__
    return await application(scope, receive, send)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\sessions.py", line 263, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\auth.py", line 185, in __call__
    return await super().__call__(scope, receive, send)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\middleware.py", line 24, in __call__
    return await self.inner(scope, receive, send)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\routing.py", line 116, in __call__
    return await application(
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\consumer.py", line 94, in app
    return await consumer(scope, receive, send)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\consumer.py", line 58, in __call__
    await await_many_dispatch(
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\utils.py", line 57, in await_many_dispatch
    await task
  File "D:\Projects\New Backend\venv\lib\site-packages\channels\utils.py", line 49, in await_many_dispatch
    result = task.result()
  File "D:\Projects\New Backend\venv\lib\site-packages\channels_redis\core.py", line 367, in receive
    message_channel, message = await self.receive_single(
  File "D:\Projects\New Backend\venv\lib\site-packages\channels_redis\core.py", line 422, in receive_single
    content = await self._brpop_with_clean(
  File "D:\Projects\New Backend\venv\lib\site-packages\channels_redis\core.py", line 255, in _brpop_with_clean
    connection = self.connection(index)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels_redis\core.py", line 738, in connection
    self.pools[index] = self.create_pool(index)
  File "D:\Projects\New Backend\venv\lib\site-packages\channels_redis\core.py", line 133, in create_pool
    return aioredis.ConnectionPool.from_url(host["address"])
  File "D:\Projects\New Backend\venv\lib\site-packages\redis\asyncio\connection.py", line 1284, in from_url
    url_options = parse_url(url)
  File "D:\Projects\New Backend\venv\lib\site-packages\redis\asyncio\connection.py", line 1176, in parse_url
    parsed: ParseResult = urlparse(url)
  File "C:\Users\luixg\AppData\Local\Programs\Python\Python39\lib\urllib\parse.py", line 392, in urlparse
    url, scheme, _coerce_result = _coerce_args(url, scheme)
  File "C:\Users\luixg\AppData\Local\Programs\Python\Python39\lib\urllib\parse.py", line 128, in _coerce_args
    return _decode_args(args) + (_encode_result,)
  File "C:\Users\luixg\AppData\Local\Programs\Python\Python39\lib\urllib\parse.py", line 112, in _decode_args
    return tuple(x.decode(encoding, errors) if x else '' for x in args)
  File "C:\Users\luixg\AppData\Local\Programs\Python\Python39\lib\urllib\parse.py", line 112, in <genexpr>
    return tuple(x.decode(encoding, errors) if x else '' for x in args)
AttributeError: 'int' object has no attribute 'decode'
WebSocket DISCONNECT /ws/chat/hello/ [127.0.0.1:65014]

I'm trying with:

  1. Django=4.1.2 channels=4.0.0 daphne=4.0.0 python=3.9.10

I've been trying for days to see the error but nothing is wrong, my only guess is that there might be a conflict with the versions of the libraries I'm using.


Solution

  • My problem was easy:

    Daphne (locally) works differently on windows than on Ubuntu:

    For Ubuntu this configuration is used in the settings.py:

    ASGI_APPLICATION = "mysite.asgi.application"
    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels_redis.core.RedisChannelLayer",
            "CONFIG": {
                "hosts": [("127.0.0.1", 6379)],
            },
        },
    }
    

    But for Windows this configuration is used:

    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels.layers.InMemoryChannelLayer"
        },
    }
    

    This is because of the way Redis works. However, this shouldn't be the functionality for a production server, I only use it to work locally.

    Although it also has to do a lot with the versions that the channels packages work with.

    I also solved this by running the following command:

    python -m pip install -U channels["daphne"]
    

    References: ASGI_APPLICATION not working with Django Channels