Overview
The Conversations API enables direct messaging between users on the platform. It supports one-on-one conversations with text messages and file attachments.
Queries
getConverstion
Retrieve a conversation between two users with all messages.
Input Parameters:
Response:
Returns a conversation object with:
ID of conversation initiator
ID of conversation recipient
Sender user details (id, name, email, username)
Receiver user details (id, name, email, username)
Array of message objects ordered by creation time (ascending)
Conversation creation timestamp
Errors:
NOT_FOUND: Conversation not found
Example:
const conversation = await trpc.conversation.getConverstion.query({
userId: "user_clxxx1",
otherUserId: "user_clxxx2"
});
console.log(`Found ${conversation.messages.length} messages`);
Mutations
sendMessage
Send a new message in an existing conversation with optional file attachments.
Input Parameters:
Array of attachment objects (can be empty)
Response:
Returns the created message with user details.
Example:
// Send a text message
const message = await trpc.conversation.sendMessage.mutate({
conversationId: "conv_clxxx...",
content: "Hi! I'm interested in your web development gig.",
attachements: []
});
// Send a message with attachments
const messageWithFiles = await trpc.conversation.sendMessage.mutate({
conversationId: "conv_clxxx...",
content: "Here are the project requirements",
attachements: [
{
name: "requirements.pdf",
url: "https://khedma-market.s3.amazonaws.com/user-id/requirements.pdf"
},
{
name: "mockups.fig",
url: "https://khedma-market.s3.amazonaws.com/user-id/mockups.fig"
}
]
});
Message Model
Message Fields
Unique message identifier (CUID)
ID of user who sent the message
Message creation timestamp
Relations
User who sent the message
Files attached to the message
Conversation Model
Conversation Fields
Unique conversation identifier (CUID)
ID of conversation initiator
ID of conversation recipient
Conversation creation timestamp
Relations
User who initiated the conversation
User who received the conversation
All messages in the conversation
Server-Side Functions
getOrCreateConversationWithMessages(user1Id, user2Id)
Get existing conversation or create a new one if it doesn’t exist.
Parameters:
user1Id: First user ID
user2Id: Second user ID
Returns: Conversation object with all messages
Example:
import { getOrCreateConversationWithMessages } from "@/server/api/routers/conversation";
const conversation = await getOrCreateConversationWithMessages(
"user_clxxx1",
"user_clxxx2"
);
getUserConversations(userId)
Fetch all conversations for a user.
Parameters:
userId: User ID to fetch conversations for
Returns: Array of conversations with messages and participant details
Note: Automatically formats conversations so the requesting user is always the “sender”.
Example:
import { getUserConversations } from "@/server/api/routers/conversation";
const conversations = await getUserConversations("user_clxxx...");
for (const conv of conversations) {
const lastMessage = conv.messages[conv.messages.length - 1];
console.log(`
With: ${conv.receiver.name}
Last message: ${lastMessage.content}
Time: ${lastMessage.createdAt}
`);
}
getConversationById(id)
Fetch a specific conversation by ID.
Parameters:
Returns: Conversation object with messages
Example:
import { getConversationById } from "@/server/api/routers/conversation";
const conversation = await getConversationById("conv_clxxx...");
getOrCreateConversation(username, currentUser)
Get or create a conversation with a user by their username.
Parameters:
username: Username of the other user
currentUser: Current authenticated user object
Returns: Conversation object or null if user not found
Example:
import { getOrCreateConversation } from "@/server/api/routers/conversation";
import { getServerAuthSession } from "@/server/auth";
const session = await getServerAuthSession();
const conversation = await getOrCreateConversation(
"johndoe",
session.user
);
Attachment Model
Messages can include file attachments:
File type (e.g., “document”, “image”, “video”)
Complete Example: Messaging Flow
// 1. Client wants to contact a freelancer
import { getOrCreateConversation } from "@/server/api/routers/conversation";
import { getServerAuthSession } from "@/server/auth";
const session = await getServerAuthSession();
const conversation = await getOrCreateConversation(
"freelancer_username",
session.user
);
// 2. Send initial message
const firstMessage = await trpc.conversation.sendMessage.mutate({
conversationId: conversation.id,
content: "Hello! I saw your React development gig and I'm interested.",
attachements: []
});
// 3. Freelancer responds with file
const response = await trpc.conversation.sendMessage.mutate({
conversationId: conversation.id,
content: "Great! Here's my portfolio for your review.",
attachements: [
{
name: "portfolio.pdf",
url: "https://s3.amazonaws.com/portfolio.pdf"
}
]
});
// 4. Retrieve full conversation history
const fullConversation = await trpc.conversation.getConverstion.query({
userId: session.user.id,
otherUserId: conversation.receiverId
});
console.log(`Total messages: ${fullConversation.messages.length}`);
// 5. List all user conversations
const allConversations = await getUserConversations(session.user.id);
console.log(`Active conversations: ${allConversations.length}`);