Giter Club home page Giter Club logo

Comments (5)

gabriel-gn avatar gabriel-gn commented on July 17, 2024 1

+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.

ferndot avatar ferndot commented on July 17, 2024 1

@gabriel-gn

  1. I like to use either models.py or pydantic_models.py. You would then import the pydantic models from there and use them in the MessageHandler.models configuration option.
  2. 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.

ferndot avatar ferndot commented on July 17, 2024

@gabriel-gn thanks, those are great ideas. Do you need anything in particular to get started?

from django-channels-handlers.

gabriel-gn avatar gabriel-gn commented on July 17, 2024

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.

gabriel-gn avatar gabriel-gn commented on July 17, 2024

Thanks a lot! I will try those 😄

from django-channels-handlers.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.