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
See Custom Consumer for full usage guidance, all handler method names, and event mapper examples.
Lifecycle methods — always call super() when overriding:
connect()— authenticate, register session, restore group memberships, dispatch pending notificationsdisconnect(close_code)— clean up expired sessions and remove from groupsreceive(text_data, bytes_data)— parse JSON, look upevent_typein the event map, call handler
Group and channel helpers:
send_group(group, event_type, data)— broadcast to a channel layer groupadd_channel_to_group(group, user_id=None)— add all active sessions for a user to a groupdiscard_channel_from_group(group, user_id=None)— remove all active sessions for a user from a groupchannel_setup()— fetchuser_groupsfrom cache and wire current channel name into each groupchannel_cleanup()— remove expired session channel names from all groups
Consumer attributes (available after connect()):
self.user— authenticated Django userself.session— currentSessioninstanceself.channel_name— unique channel name for this connection; changes on every reconnectself.channel_layer— configured channel layer instanceself.permission_handler— instance of the configuredPermissionHandlerclass
Event mapper:
realtime_chat_messaging.variables.consumers.map_event_type_to_handlers
The default function that builds the event_type to handler method routing
table used by receive(). Pass a custom mapper via EVENT_MAPPER in
settings to add, restrict, or replace events. See Custom Consumer for
the full default mapping table and customization examples.
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