Skip to main content

Overview

The ChatRoom model represents a conversation space where multiple users can exchange messages. Each chat room has a unique identifier, a name, and contains collections of messages and participants.

Model Purpose

ChatRooms serve as the primary organizational unit for conversations in the RealtimeChat application. They:
  • Group related messages together
  • Define the scope of real-time subscriptions
  • Manage participant membership
  • Provide context for message delivery

Properties

Id
integer
required
Unique identifier for the chat room.
Name
string
required
Display name for the chat room. Used in UI to identify the conversation.
Messages
array
Collection of all messages posted in this chat room.

Relationships

Messages

Each chat room has a one-to-many relationship with messages. Messages are automatically associated with their parent chat room through the ChatRoomId foreign key.
The GraphQL API returns messages with each chat room, but participant information is managed separately in the database layer and not exposed through the current GraphQL schema.

GraphQL Type Definition

type ChatRoom {
  id: Int!
  name: String!
  messages: [Message!]!
}
The GraphQL representation includes Id, Name, and the nested Messages collection. Participants are managed separately through dedicated queries and mutations.

Example JSON Representation

{
  "id": 1,
  "name": "General Discussion",
  "messages": [
    {
      "id": 101,
      "sentAt": "2026-03-05T14:30:00Z",
      "senderId": "user-abc-123",
      "chatRoomId": 1,
      "content": {
        "text": "Hello everyone!"
      }
    },
    {
      "id": 102,
      "sentAt": "2026-03-05T14:31:15Z",
      "senderId": "user-xyz-789",
      "chatRoomId": 1,
      "content": {
        "url": "https://example.com/image.jpg",
        "caption": "Check out this picture"
      }
    }
  ]
}

Database Schema

The chat room is persisted using the ChatRoomEntity class:
public class ChatRoomEntity
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public ICollection<MessageEntity> Messages { get; set; } = null!;
    public ICollection<ChatRoomParticipantEntity> ChatRoomParticipants { get; set; } = null!;
}
The database schema includes navigation properties for both Messages and ChatRoomParticipants to support efficient querying and eager loading.

Usage in GraphQL

Chat rooms are the primary entry point for querying conversation data:
query GetChatRoom {
  chatRoom(id: 1) {
    id
    name
    messages {
      id
      sentAt
      senderId
      content {
        ... on TextMessageContent {
          text
        }
        ... on ImageMessageContent {
          url
          caption
        }
      }
    }
  }
}

Best Practices

  • Naming: Use descriptive, user-friendly names for chat rooms
  • Participants: Always verify user membership before allowing message posting
  • Message Loading: Consider pagination for chat rooms with large message histories
  • Real-time Updates: Subscribe to chat room updates to receive new messages immediately

Build docs developers (and LLMs) love