Skip to main content

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 the Authorization 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
Snapshot of the most recent message in the conversation
string
Message content
string
ObjectId of the user who sent the message
string
ISO 8601 timestamp when the message was sent
boolean
Whether the message has been read by the recipient
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
Array of populated user objects
string
User’s unique identifier
string
User’s full name
string
URL to the user’s profile picture
object
Seller profile information
boolean
Whether the seller has a verified badge
object
Populated horse object (if conversation is linked to a horse)
string
Horse’s unique identifier
string
Horse’s name
array
Array of horse photos
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.
Event Payload:

Message Notification Event

Receive notifications when someone sends you a message (delivered to your personal user room).
Event Payload:
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_message field is automatically updated when new messages are sent

Indexes

  • participants: Used for efficiently finding all conversations for a user