API Reference¶
This page documents the public interfaces of every major component. Use this as a reference when subclassing or extending the package.
EventHandler¶
realtime_chat_messaging.utils.handlers.EventHandler
The aggregate handler combining all domain mixins. Each operation exists as an async/sync pair. Override the async method for extensions, side effects, and async integrations. Override the sync helper only when you need to replace or heavily modify the core DB logic. See Custom Handlers for full guidance.
Message methods
Async method (extend here) |
Sync helper (replace here) |
Returns |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Room methods
Async method (extend here) |
Sync helper (replace here) |
Returns |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Session methods
Async method (extend here) |
Sync helper (replace here) |
Returns |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Notification methods
Async method (extend here) |
Sync helper (replace here) |
Returns |
|---|---|---|
|
|
|
|
called synchronously inside |
|
|
called synchronously inside |
|
PermissionHandler¶
realtime_chat_messaging.permissions.handlers.PermissionHandler
All methods are async wrappers around synchronous _have_* helpers. All
return (bool, Room) except have_message_permission which returns bool.
Async method |
Returns |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
ExceptionHandler¶
realtime_chat_messaging.utils.decorators.ExceptionHandler
Method |
Description |
|---|---|
|
Decorator wrapping async consumer methods. Catches all Django/DRF exceptions and sends structured error JSON. |
|
Send a structured error payload to the client and log the traceback. |
Exception → code mapping:
DRFValidationError→4003DjangoValidationError→4003IntegrityError→4005ObjectDoesNotExist→4004Http404→4004PermissionDenied→4002Exception(all others) →4006
ChatMessagingConsumer¶
realtime_chat_messaging.consumers.ChatMessagingConsumer
Lifecycle methods (always call super() when overriding):
connect()— authenticate, cleanup expired sessions, set up groupsdisconnect(close_code)— cleanup expired sessionsreceive(text_data, bytes_data)— parse JSON, route via EVENT_MAPPER
Group helpers:
send_group(group, event_type, data)— broadcast to a channel groupadd_channel_to_group(group, user_id=None)— add all user sessions to a groupdiscard_channel_from_group(group, user_id=None)— remove all user sessions from a group
Session helpers:
channel_setup()— restore group memberships and register sessionchannel_cleanup()— remove expired sessions from groups
Consumer attributes (available after connect()):
self.user— authenticated Django userself.session— currentSessioninstanceself.channel_name— this connection’s channel nameself.channel_layer— Django Channels layer
Loader Utilities¶
realtime_chat_messaging.utils.loader
Functions for dynamically resolving configured models and serializers:
from realtime_chat_messaging.utils.loader import get_model, get_serializer
Message = get_model("Message") # Returns configured Message class
Serializer = get_serializer("MessageSerializer") # Returns configured class
get_model(name: str) → Model— look up a model by its settings keyget_serializer(name: str) → Serializer— look up a serializer by its settings keyimport_and_verify_type_class(klass, repr, class_instance=None)— import and validate a class from a dotted pathimport_and_verify_type_function(func, repr)— import and validate a function from a dotted pathclear_caches()— clear model and serializer caches (used in tests)
Settings¶
realtime_chat_messaging.conf.realtime_chat_settings
The global settings instance. Access any setting as an attribute:
from realtime_chat_messaging.conf import realtime_chat_settings
threshold = realtime_chat_settings.INACTIVITY_THRESHOLD
soft_delete = realtime_chat_settings.MESSAGE_SOFT_DELETE
See Settings Reference for all available settings and their defaults.
Cache Utilities¶
realtime_chat_messaging.utils.cache_utils
Async-safe functions for user group membership cache:
from realtime_chat_messaging.utils.cache_utils import (
fetch_user_groups,
update_user_groups,
add_group_to_user_groups,
remove_group_from_user_groups,
)
All functions are decorated with @sync_to_async and accept user_id as a
string. Cache keys follow the format user:<user_id>:groups with no
expiration. Values are JSON-serialised lists of group name strings.
WebSocket Close Codes¶
See Error Codes for the full table. Summary:
4001— unauthenticated (connection closed)4002— permission denied4003— validation error4004— resource not found4005— integrity/constraint error4006— internal server error