Comments (5)
+1 this. How to integrate it effectvely in django? How can I replace the default channels tutorial with channels-handlers? Thanks!
from django-channels-handlers.
- I like to use either
models.py
orpydantic_models.py
. You would then import the pydantic models from there and use them in theMessageHandler.models
configuration option. - This is definitely a part that I would like to improve on more. This particular setup depends on how you wish to model your channel groups.
For instance, I chose to have a single channel that each user would listen on (an inbox of sorts). Consequently, I designed the interface to be the following:
class ChatConsumer(AsyncConsumerHandlerMixin, AsyncJsonWebsocketConsumer):
handler_classes = [ChatHandler]
...
async def send_message(self, message, user_ids):
# Serialize message
serialized_message = self.serialize_message(message)
# Send message to room group
for user_id in user_ids:
await self.channel_layer.group_send(
f"inbox_{user_id}",
{"type": "message.outgoing", "content": serialized_message},
)
async def message_outgoing(self, event):
"""
Sends a message to the participant
"""
# Deserialize message
message = self.deserialize_message(event["content"])
# Send message to WebSocket
await self.send_json(content=message)
This can be used in the following manner from within the handler:
# Send typing notification to all participants
output_message = self.serialize_message(
"chat.typing",
{"thread": message.thread, "sender": self.get_current_user().pk},
)
participants = await retrieve_thread_participants(message.thread)
await self.consumer.send_message(output_message, participants)
Does that help?
from django-channels-handlers.
@gabriel-gn thanks, those are great ideas. Do you need anything in particular to get started?
from django-channels-handlers.
Where do I create pydantic models for channels handlers in my custom channels app and where do I call them? (assuming I have a "routing.py" and "consumers.py" files)
How can I send a message with what I have without needing to use
channel_layer = get_channel_layer(); async_to_sync(channel_layer.group_send)("some_channel", {"type": "channel_message", "message": message_content} )
?
from django-channels-handlers.
Thanks a lot! I will try those
from django-channels-handlers.
Related Issues (3)
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 django-channels-handlers.