Live Chat ~ Consumers.py Explained - uchicago-cs/chigame GitHub Wiki
Consumers.py Explained
consumers.py
implements real-time WebSocket communication using Django Channels (channels
library). The class, ChatConsumer
enables bidirectional, persistent connections between users in a chat room.
Key Role/Functionality
- Handles WebSocket connections, disconnects, recievals
- Uses Django
channels
group system to broadcast members to all members connected to channel - Can also perform async database operations with
@database_sync_to_async
- This is used to do things like store messages in the database, for example, without affecting channel performance (by performing these actions asynchronously)
- Connection functions include auth checks
Core Functions
These are functions that determine the majority of the classes' functionality as listed above. There are more, however, these are the main ones you need to understand to then expand or extend this class.
connect()
: Performs user auth, then adds the user to a channel groupdisconnect()
: Remove user from channel grouprecieve()
: Processes incoming WebHook messages and processes them (at the moment, this also includes saving them to the database, for example)sendMessage()
: Broadcasts messages to all the connected clients on the channel
Updating This Documentation
If you read this documentation and you decide to change core functionality please update this page to reflect your changes!