#AttributeError: 'set' object has no attribute 'decode'

34 messages · Page 1 of 1 (latest)

pulsar cairn
#

Hi, I started learning Django for a school project and I'm doing a chat project using websocket server, but when reloading the app I get this error. I'm using Django Channels with Daphne.

my consumers.py```py
from channels.generic.websocket import WebsocketConsumer

class ChatConsumer(WebsocketConsumer):
def connect(self):
self.accept()

def disconnect(self, code):
    pass
my routing.py```py
from django.urls import path
from . import consumers

websocket_urlpatterns = [
  path('chat/', consumers.ChatConsumer.as_asgi())
]```
my asgi.py```py
import chat.routing
import os

from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django_channels_jwt_auth_middleware.auth import JWTAuthMiddlewareStack
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

application = ProtocolTypeRouter({
    'http': get_asgi_application(),
    'websocket': AllowedHostsOriginValidator(
        JWTAuthMiddlewareStack(
            URLRouter(chat.routing.websocket_urlpatterns)
        )
    )
})
#

this is the traceback```py
Exception inside application: 'set' object has no attribute 'decode'
File "/env/lib/python3.12/site-packages/django/contrib/staticfiles/handlers.py", line 101, in call
return await self.application(scope, receive, send)
^^^^^^^^^^
File "/env/lib/python3.12/site-packages/channels/routing.py", line 62, in call
return await application(scope, receive, send)
^^^^^^^^^^
File "/env/lib/python3.12/site-packages/channels/security/websocket.py", line 37, in call
return await self.application(scope, receive, send)
^^^^^^^^^^

#
  File "/env/lib/python3.12/site-packages/django_channels_jwt_auth_middleware/auth.py", line 36, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^
  File "/env/lib/python3.12/site-packages/channels/sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
           ^^^^^^^^^^
  File "/env/lib/python3.12/site-packages/channels/sessions.py", line 263, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
           ^^^^^^^^^^
  File "/env/lib/python3.12/site-packages/channels/auth.py", line 185, in __call__
    return await super().__call__(scope, receive, send)
           ^^^^^^^^^^
  File "/env/lib/python3.12/site-packages/channels/middleware.py", line 24, in __call__
    return await self.inner(scope, receive, send)
           ^^^^^^^^^^
  File "/env/lib/python3.12/site-packages/channels/routing.py", line 116, in __call__
    return await application(
           ^^^^^^^^^^
  File "/env/lib/python3.12/site-packages/channels/consumer.py", line 94, in app
    return await consumer(scope, receive, send)
           ^^^^^^^^^^
  File "/env/lib/python3.12/site-packages/channels/consumer.py", line 58, in __call__
    await await_many_dispatch(
  File "/env/lib/python3.12/site-packages/channels/utils.py", line 57, in await_many_dispatch
    await task
AttributeError: 'set' object has no attribute 'decode'
```pt.2, I had to split because I couldn't send it
golden frost
#

looks like nobody's offered to help for an hour ... I'll help if you do a bunch of preliminary work:

  • put your code on github where I can get it. all of it
  • be prepared to explain in detail how I should run your code in order to see what you're seeing
    In otherwords, I'll take a look if you make it easy for me
#

if I have to spend five minutes asking you questions like "OK it's running, now what do I do" in order to reproduce your problem, I will probably give up.

pulsar cairn
#

to setup:
virtualenv -p python3 env
source env/bin/activate
pip install -r requirements.txt
deactivate
cd app
npm install

to run:
Terminal 1- npm run start, open android or ios simulator
Terminal 2- make server
Terminal 3- make redis

#

After you login in the app, reload the app and the error will appear in the terminal

golden frost
#

gosh! Ok I'll try all that stuff

pulsar cairn
#

thank you!

golden frost
#

"open android or ios simulator"? Does that mean I need to have previously installed one of those things?

pulsar cairn
#

yes, if you have a mac you can install xcode to open the ios simulator, or you can just download android studio and create android simulator

golden frost
#

sorry I don't want to do all that 😦 Those things are huge

pulsar cairn
#

the same error will appear in terminal 2

pulsar cairn
#

you can use this url to send the request

golden frost
#

Maybe in 10 minutes

pulsar cairn
#

no problem

golden frost
#

make server failed with ModuleNotFoundError: No module named 'channels_auth_token_middlewares'

pulsar cairn
#

Instead of pip install -r requirements.txt do pip install channels-auth-token-middlewares now

golden frost
#

well I already did -r requirements.txt

pulsar cairn
#

Yeah but i forgot to add the channels... to the file

#

That's why you need to run pip install channels...

golden frost
#

ok the server is running now ... on to the next step.

pulsar cairn
#

On postman create a websocket request

golden frost
#

ok redis is running

golden frost
#

what does "on postman" mean

pulsar cairn
#

Postman the software to make request

golden frost
#

I think we've hit my threshold for laziness, sorry

pulsar cairn
#

It's really fast to download

median escarp
#

Random shot:

CHANNEL_LAYERS = {
  'default': {
    'BACKEND': 'channels_redis.core.RedisChannelLayer',
    'CONFIG': {
-      'hosts': [{'127.0.0.1', 6379}]
+      'hosts': [('127.0.0.1', 6379)]
    }
  }
}