Overview
TheMessage model represents individual messages sent within chat rooms. Each message contains metadata (sender, timestamp, chat room) and polymorphic content that can be either text or image-based.
Model Purpose
Messages are the core communication unit in RealtimeChat. They:- Capture user-generated content (text or images)
- Track message metadata (sender, timestamp, location)
- Support real-time delivery through GraphQL subscriptions
- Enable full-text search and message history
Properties
Unique identifier for the message.
UTC timestamp indicating when the message was sent. Used for message ordering and display.
ID of the user who sent the message. References the
ApplicationUser.Id field.ID of the chat room where this message was posted. Defines the message’s conversation context.
Polymorphic message content. Can be either
TextMessageContent or ImageMessageContent.Message Content Types
Messages support two types of content through a union type pattern:TextMessageContent
Plain text messages containing user-written content.ImageMessageContent
Image-based messages with an optional caption.The content type is determined at runtime using GraphQL union types. Clients must use inline fragments to query specific content types.
Relationships
Sender (User)
Each message belongs to a single user identified bySenderId. This references the ApplicationUser model and enables:
- User attribution for messages
- Message history per user
- Authorization checks
Chat Room
Messages are always associated with a specific chat room throughChatRoomId. This relationship:
- Organizes messages into conversations
- Defines subscription scope
- Enables room-based message queries
GraphQL Type Definition
The GraphQL schema uses a union type for
MessageContent, allowing clients to handle different message types appropriately.Example JSON Representations
Text Message
Image Message with Caption
Image Message without Caption
Database Schema
Messages are persisted using theMessageEntity class:
The database entity includes a
ContentJson computed property for serializing message content, and navigation properties for User and ChatRoom relationships.Full-Text Search Capability
The message model supports full-text search, enabling users to:- Search message content across chat rooms
- Find historical conversations
- Locate specific information quickly
Search functionality is optimized for text content. Image captions are also searchable when present.
GraphQL Query Examples
Query Messages with Content Type Handling
Subscribe to New Messages
Best Practices
- Timestamps: Always use UTC for
SentAtto avoid timezone issues - Content Validation: Validate text length and image URLs before persisting
- Authorization: Verify user membership in chat room before allowing message creation
- Image Storage: Store images externally and reference by URL for better performance
- Content Type Handling: Use GraphQL inline fragments to properly handle different content types