Rooms¶
A room is the container for a conversation. There are three concrete room
types, all inheriting from the polymorphic Room base model. Every room has a
UUID primary key, created_at, updated_at, a last_message foreign key,
and a one-to-one property (RoomProperty) for storing preferences.
Room Types¶
OneToOneChat¶
A private conversation between exactly two users. Enforced at the database
level by signals — adding more or fewer than two participants raises a
ValidationError.
Field |
Description |
|---|---|
|
M2M to |
Participants cannot leave a
OneToOneChat(room.leaveraises aValidationError). If a participant’s user account is deleted, a pre_delete signal on theUsermodel handles cleanup by deleting anyOneToOneChatthe user belongs to. This is a known temporary fix for the current release — Django’s M2Mpost_removesignal does not fire whenCASCADEdeletion propagates through the through table, so direct room cleanup is handled at the User pre-delete stage instead. If you are using a customOneToOneChatmodel, this cleanup is delegated to you.Only participants can send messages.
Duplicate chats between the same two users are prevented by signals.
GroupChat¶
A multi-user chat room with admin controls, a member cap, and optional message locking.
Field |
Description |
|---|---|
|
Display name (max 64 chars). Required. |
|
Optional text description. |
|
The user who created the group. Always an admin. Cannot be removed by other admins. |
|
M2M to |
|
M2M to |
|
Enforced by signals. Default 100. |
|
Optional URL to a group image. |
|
Boolean flag. See note below. |
|
When |
Joining a GroupChat: The default behaviour raises a ValidationError with
the message “Ask an admin to add you to the group”. Groups are joined by having
an admin call room.add_members. The join_approval_required flag exists to
support custom approval flows — set it to True when creating the room and
override join_room in a custom EventHandler to implement your approval
logic. See Custom Handlers for an example.
Locking a group: Set group_locked: true via room.modify with
action: "update" to restrict sending to admins only. This can also be toggled
back off with group_locked: false.
Automatic deletion: When the last participant leaves or is removed, signals delete the room automatically.
Channel¶
A broadcast room where only moderators (and users with explicit permission) can send messages by default. Suited for announcements, news feeds, or one-to-many communication.
Field |
Description |
|---|---|
|
Display name (max 64 chars). Required. |
|
Optional text description. |
|
The user who created the channel. Always a moderator. |
|
M2M to |
|
M2M to |
|
When |
|
Optional URL to a channel image. |
|
Enforced by signals. Default 300. |
Sending to a Channel: Only the creator, moderators, and users with the
can_send_messages object-level permission can post. Grant this permission via
room.modify with action: "add_permission" and permissions:
["can_send_messages"].
Joining a Channel: If is_public is True, any authenticated user can
join using the room.join event. Private channels require a moderator to add
them via room.add_members.
RoomProperty¶
Every room is automatically created with a RoomProperty instance (via
pre_save signal) that stores a freeform preferences JSON field. Use this
to store any room-level settings your application needs without modifying the room
model directly.
# Example preferences dict
{
"notifications": True,
"theme": "dark",
"pinned_message_ids": ["uuid1", "uuid2"]
}
Access and update preferences via the room.modify event with action:
"update" and a property key in the data. See WebSocket Events for
the full payload structure.
Permissions Summary¶
Permission |
Room type |
Granted to |
|---|---|---|
|
GroupChat |
Creator, admins |
|
GroupChat |
Creator, admins |
|
Channel |
Creator, moderators |
|
Channel |
Creator, moderators |
|
Channel |
Creator, moderators (and explicit grants) |
Permissions are granted and revoked automatically via Django signals when users
are added to or removed from the admins or moderators M2M fields. They
can also be managed granularly via room.modify. All permissions use
django-guardian for object-level enforcement.
Creating Rooms¶
Rooms are created via the WebSocket room.create event. See
WebSocket Events for complete payload documentation. Key points:
The creator is automatically added as a participant/subscriber and admin/moderator by signals — you do not need to include them in the
participantsorsubscriberslist.For
OneToOneChat, the participants list should contain only the other user’s ID. The creator is added automatically, making the total count exactly 2.Room
propertyandpreferencesare optional at creation time. Defaults are applied if not provided.