Skip to main content
Private chats enable direct one-on-one messaging between users in your application. WireChat automatically handles conversation creation, message routing, and participant management.

Creating Private Conversations

Create a private conversation between two users using the createConversationWith() method:
use App\Models\User;

$currentUser = auth()->user();
$otherUser = User::find(2);

// Create or retrieve existing conversation
$conversation = $currentUser->createConversationWith($otherUser);

// Create with an initial message
$conversation = $currentUser->createConversationWith(
    $otherUser, 
    'Hello! How are you?'
);
WireChat automatically detects existing conversations between two users and returns the existing conversation instead of creating duplicates.

Self Conversations

Users can create conversations with themselves for notes or reminders:
// Create a self conversation
$selfConversation = $currentUser->createConversationWith($currentUser);
The system automatically detects when both participants are the same user and creates a SELF conversation type instead of PRIVATE.

Sending Messages

Send messages in private conversations using the sendMessageTo() method:
// Send to a user (creates conversation if needed)
$message = $currentUser->sendMessageTo($otherUser, 'Hi there!');

// Send to an existing conversation
$message = $currentUser->sendMessageTo($conversation, 'Another message');

Conversation Detection

Check if a conversation already exists between two users:
if ($currentUser->hasConversationWith($otherUser)) {
    // Conversation exists
    $conversation = $currentUser->createConversationWith($otherUser);
}

Private Chat Features

Automatic Deduplication

WireChat prevents duplicate private conversations between the same two users

Read Receipts

Track when messages are read with conversation-level read timestamps

Message Deletion

Delete messages for yourself or for everyone in the conversation

Real-time Updates

Messages appear instantly using Laravel Echo and broadcasting

Reading Messages

Mark conversations as read and track unread messages:
// Mark conversation as read for current user
$conversation->markAsRead($currentUser);

// Check if conversation has been read
if ($conversation->readBy($currentUser)) {
    // User has read all messages
}

// Get unread messages
$unreadMessages = $conversation->unreadMessages($currentUser);
$unreadCount = $conversation->getUnreadCountFor($currentUser);

Deleting Conversations

1

Delete for One User

Remove the conversation from one user’s view while keeping it for the other:
$conversation->deleteFor($currentUser);
// or
$currentUser->deleteConversation($conversation);
2

Automatic Cleanup

When both users in a private conversation delete it, WireChat automatically removes it from the database:
// src/Models/Conversation.php:676-680
// If all participants have deleted the conversation, force delete it
if ($deletedByBothParticipants) {
    return $this->forceDelete();
}

Clearing Conversation History

Clear message history without deleting the conversation:
// Clear all messages for current user
$conversation->clearFor($currentUser);
// or
$currentUser->clearConversation($conversation);

// Messages are hidden but can reappear if new messages arrive

Participant Management

Access participant information in private chats:
// Get the other participant in a private chat
$peerParticipant = $conversation->peerParticipant($currentUser);
$peerUser = $peerParticipant->participantable;

// Get current user's participant record
$authParticipant = $conversation->participant($currentUser);

// Get both participants
$allParticipants = $conversation->participants;

Conversation Types

WireChat distinguishes between private and self conversations:
// Check conversation type
if ($conversation->isPrivate()) {
    // One-on-one conversation
}

if ($conversation->isSelf()) {
    // Self conversation (notes)
}

if ($conversation->isGroup()) {
    // Group conversation
}

Privacy & Permissions

Verify user access before showing conversation content:
// Check if user belongs to conversation
if ($currentUser->belongsToConversation($conversation)) {
    // User has access to this conversation
}

// This check is automatically performed in WireChat components
// src/Livewire/Chat/Chat.php:796
abort_unless($this->auth->belongsToConversation($this->conversation), 403);
All WireChat Livewire components automatically verify that the authenticated user belongs to the conversation before displaying any data.

Last Message & Activity

Track conversation activity:
// Get the last message
$lastMessage = $conversation->lastMessage;

// Get conversation update timestamp
$lastActivity = $conversation->updated_at;

// Update participant's last active timestamp
$participant->update(['last_active_at' => now()]);

Next Steps

Attachments

Learn how to send files and media in private chats

Notifications

Set up real-time notifications for new messages

Group Chats

Create multi-user group conversations

Search

Implement chat and user search functionality