Skip to main content

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:
Socket.io:

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
Populated sender user object
string
User’s unique identifier
string
User’s full name
string
URL to the user’s profile picture
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
The created message object
string
Unique message identifier
string
ID of the conversation
string
ObjectId of the authenticated user who sent the message
string
Message content
boolean
Always false for newly created messages
string
ISO 8601 timestamp when the message was sent
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:
Event: send_message
string
required
The ID of the conversation to send the message to
string
required
The message content (maximum 2000 characters)
Acknowledgment Response:

Receive New Messages

Listen for new messages in conversations you’ve joined:
Event Payload:
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):
Event Payload:
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:
Typing Event Payloads:

Data Model

Message Schema

Validation Rules

  • text field is required and limited to 2000 characters
  • conversation_id must reference a valid conversation
  • sender_id must 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_message field is updated
  • The sent_at timestamp is automatically set on creation
  • Messages with deleted_at set are excluded from all queries

Complete Example

Real-time Chat Implementation