Overview
Messages are individual chat messages within a conversation. The Horse Trust platform supports both REST API and Socket.io for sending and receiving messages in real-time.Authentication
All message endpoints require authentication via JWT token. REST API:Get Messages
GET /api/chat/conversations/:id/messages
Retrieve paginated message history for a conversation. Messages are returned in chronological order (oldest first). When you fetch messages, all unread messages from other users are automatically marked as read.
string
required
The conversation ID to retrieve messages from
string
default:"1"
Page number for pagination (minimum: 1)
string
default:"30"
Number of messages per page (maximum: 100)
Response Fields
boolean
required
Indicates if the request was successful
array
Array of message objects in chronological order (oldest first)
string
Unique message identifier
string
ID of the conversation this message belongs to
object
string
Message content (maximum 2000 characters)
boolean
Whether the message has been read by the recipient
string
ISO 8601 timestamp when the message was read (null if unread)
string
ISO 8601 timestamp when the message was sent
string
Error message (only present when success is false)
Fetching messages automatically marks unread messages from other participants as read and sets the
read_at timestamp.Error Responses
- 404 Not Found: Conversation not found or user is not a participant
- 500 Internal Server Error: Server error occurred
Send Message (REST)
POST /api/chat/conversations/:id/messages
Send a message to a conversation via REST API. This endpoint serves as a fallback when Socket.io is not available. The conversation’s last_message field is automatically updated.
string
required
The conversation ID to send the message to
string
required
The message content (maximum 2000 characters)
Response Fields
boolean
required
Indicates if the request was successful
object
string
Error message (only present when success is false)
Error Responses
- 404 Not Found: Conversation not found or user is not a participant
- 500 Internal Server Error: Server error occurred
Socket.io Real-Time Messaging
Connection Setup
Socket.io authentication uses JWT tokens passed in the
auth.token field during connection. Invalid or missing tokens will result in a connection error.Join Conversation Room
Before receiving real-time messages, join the conversation room:string
required
The ID of the conversation to join
Send Message
Send a message via Socket.io with acknowledgment:send_message
string
required
The ID of the conversation to send the message to
string
required
The message content (maximum 2000 characters)
Receive New Messages
Listen for new messages in conversations you’ve joined:The
new_message event is broadcast to all users in the conversation room, including the sender.Message Notifications
Receive notifications when you receive a new message (delivered to your personal user room):string
ID of the conversation where the message was sent
string
ObjectId of the user who sent the message
string
First 60 characters of the message text
Typing Indicators
Send and receive typing indicators:Data Model
Message Schema
Validation Rules
textfield is required and limited to 2000 charactersconversation_idmust reference a valid conversationsender_idmust be a participant in the conversation- Messages are soft-deleted (using
deleted_at) and excluded from queries
Indexes
conversation_id: Used for efficiently querying messages in a conversation
Automatic Behavior
- When messages are fetched via GET endpoint, unread messages from other users are automatically marked as read
- When a message is sent, the conversation’s
last_messagefield is updated - The
sent_attimestamp is automatically set on creation - Messages with
deleted_atset are excluded from all queries

