Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better documentation #1

Open
ferndot opened this issue Aug 10, 2019 · 5 comments
Open

Better documentation #1

ferndot opened this issue Aug 10, 2019 · 5 comments
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed

Comments

@ferndot
Copy link
Owner

ferndot commented Aug 10, 2019

This project needs comprehensive documentation. Optimally, it would be stored on ReadTheDocs.

@ferndot ferndot added documentation Improvements or additions to documentation help wanted Extra attention is needed good first issue Good for newcomers labels Aug 10, 2019
@gabriel-gn
Copy link

+1 this. How to integrate it effectvely in django? How can I replace the default channels tutorial with channels-handlers? Thanks!

@ferndot
Copy link
Owner Author

ferndot commented Jan 15, 2020

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

@gabriel-gn
Copy link

gabriel-gn commented Jan 16, 2020

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} )?

@ferndot
Copy link
Owner Author

ferndot commented Jan 20, 2020

@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?

@gabriel-gn
Copy link

Thanks a lot! I will try those 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants