Comments (6)
Compare your class:
to the base class:
channels/channels/generic/websocket.py
Lines 288 to 290 in 42deaca
By overriding this method without using the async
keyword, the await
call at the error point is receiving a string, rather than a coroutine object. You want to pass UUIDEncoder
, but you still need to declare the method async def
.
from channels.
After examining the code within the parent class (AsyncJsonWebsocketConsumer), I noticed that the encoding process for the content using json.dumps(content) was utilizing the await keyword. However, I found that removing the await and simply using json.dumps resolved the error. This adjustment allowed the "welcome message" to be sent successfully to the client side without encountering any error.
from channels.
The encode_json
method is unchanged since it was introduced:
de82aaa#diff-8adaf2e6e01ec2cc05dd10bfd212d6dddfadabe0ddc679b821e03024806647b7R254-R256
@classmethod
async def encode_json(cls, content):
return json.dumps(content)
from channels.
encode_json
is defined as an async
function, but it uses json.dumps
which is a synchronous function.
Error explanation:
When we call await self.encode_json(content)
, it treats the return value of json.dumps(content)
as a coroutine, but since json.dumps
returns a string
synchronously, it raises the TypeError.
Here is the error message:
If I am misunderstanding something, please let me know.
from channels.
It looks like you've overridden a method maybe and forgotten an async
.
Please post code rather than screenshots.
The async send_json method is covered by the test suite. If you could add a test demonstrating your issue, that would help to identify what you're seeing.
from channels.
chats/consumers.py
class ChatConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
# Accept the connection
await self.accept()
# Send a welcome message
await self.send_json(
{
"type": "welcome_message",
"message": "Welcome to the Websocket Connection",
}
)
The error traceback is as follows:
python3.12/site-packages/channels/generic/websocket.py", line 282, in send_json
await super().send(text_data=await self.encode_json(content), close=close)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object str can't be used in 'await' expression
The full chats/consumers.py
file can be found here: ChatConsumer file.
Here is a video demo of the error.
Screencast.from.2024-05-20.17-22-47.webm
from channels.
Related Issues (20)
- OriginValidator could raise exception/log message for denied connections HOT 1
- Unable to collect code coverage when using `ChannelsLiveServerTestCase` HOT 14
- error in channel layer explanations HOT 2
- adding a check if group_name exists method for channel_layer HOT 1
- Small messages are getting concatenated HOT 2
- How can I change the schema in the database? HOT 1
- How to test send_json() contents when passing close=True HOT 2
- Handler declaration by decorator/annotation (in order to prevent potential method leaks) HOT 1
- django-q2 and django-channels together HOT 1
- Channel Layer's group_send() Not Working When Called From Django View HOT 3
- Add CORs middleware HOT 1
- Breakage in routing.py on django/main pre-5.1.
- Use Django async-native APIs where possible HOT 9
- Django Channels Memory Leak on every message or connection HOT 28
- WARNING daphne.server ASGIStaticFilesHandler WebRequest took too long to shut down and was killed HOT 4
- Use group_send() when group is empty HOT 1
- Session Data Corrupted HOT 1
- Getting TypeError: SSEConsumer() missing 2 required positional arguments: 'receive' and 'send'
- django.core.exceptions.SynchronousOnlyOperation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from channels.