Overview
Conversations represent chat threads between two users. Each conversation can optionally be linked to a horse listing (e.g., when a buyer contacts a seller about a specific horse).Authentication
All conversation endpoints require authentication via JWT token in theAuthorization header.
Create or Get Conversation
POST /api/chat/conversations
Creates a new conversation or returns an existing one between the authenticated user and the recipient. If a conversation already exists between these two users (optionally for the same horse), the existing conversation is returned.
string
required
MongoDB ObjectId of the user to start a conversation with. Must be a valid ObjectId and cannot be the current user’s ID.
string
MongoDB ObjectId of the horse listing this conversation is about. Optional. If provided, the conversation will be linked to this specific horse.
Response Fields
boolean
required
Indicates if the request was successful
object
The conversation object
string
Unique conversation identifier
array
Array of exactly 2 user ObjectIds representing the participants in the conversation
string
MongoDB ObjectId of the related horse listing (if applicable)
object
string
ISO 8601 timestamp when the conversation was created
string
ISO 8601 timestamp when the conversation was last updated
string
Error message (only present when success is false)
Error Responses
- 400 Bad Request: Invalid recipient ID or attempting to message yourself
- 500 Internal Server Error: Server error occurred
List Conversations
GET /api/chat/conversations
Returns all conversations for the authenticated user, sorted by most recently updated first. Results include populated participant details and horse information.
Response Fields
boolean
required
Indicates if the request was successful
array
Array of conversation objects sorted by
updated_at (most recent first)string
Unique conversation identifier
array
object
object
Most recent message in the conversation (see Create or Get Conversation for fields)
string
ISO 8601 timestamp when the conversation was created
string
ISO 8601 timestamp when the conversation was last updated
Error Responses
- 500 Internal Server Error: Server error occurred
Socket.io Events
Join Conversation Room
Join a specific conversation room to receive real-time messages.string
required
The ID of the conversation to join
New Message Event
Receive new messages in conversations you’ve joined.Message Notification Event
Receive notifications when someone sends you a message (delivered to your personal user room).The preview field contains the first 60 characters of the message text.
Data Model
Conversation Schema
Validation Rules
- Conversations must have exactly 2 participants
- Participants must be valid user ObjectIds
- Users cannot create conversations with themselves
- The
last_messagefield is automatically updated when new messages are sent
Indexes
participants: Used for efficiently finding all conversations for a user

