1. Go to this page and download the library: Download rahatulrabbi/talkbridge library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rahatulrabbi / talkbridge example snippets
'user_fields' => [
'id' => 'id',
'name' => 'name', // single column — most common
'avatar' => 'avatar_path', // your avatar column name
'last_seen' => 'last_seen_at', // your last_seen column name
'is_active' => null, // set to 'is_active' if you have this column
],
'user_fields' => [
'name' => ['first_name', 'last_name'],
// or three parts:
'name' => ['f_name', 'm_name', 'l_name'],
],
use RahatulRabbi\TalkBridge\Services\ChatService;
class YourController extends Controller
{
public function __construct(protected ChatService $chat) {}
}
use RahatulRabbi\TalkBridge\Facades\TalkBridge;
// List conversations (paginated, supports search)
$conversations = $chat->listConversations($user, perPage: 30, query: 'search term');
// Start or get a private conversation
$conversation = $chat->startConversation($user, receiverId: 5);
// Create a group
$group = $chat->createGroup($user, [
'name' => 'Project Team',
'participants' => [2, 3, 4],
'group' => ['description' => 'Our team', 'type' => 'private'],
]);
// Remove conversation from user's list (soft delete for that user only)
$chat->deleteConversationForUser($user->id, $conversationId);
// Media library for a conversation (images, video, audio, files, links)
$media = $chat->mediaLibrary($user, $conversationId, perPage: 30);
// Send a text message
$message = $chat->sendMessage($user, [
'conversation_id' => 15,
'message' => 'Hello team!',
'message_type' => 'text',
]);
// Send with a file attachment
$message = $chat->sendMessage($user, [
'conversation_id' => 15,
'message_type' => 'image',
'attachments' => [['path' => $request->file('image')]],
]);
// Reply to a message
$message = $chat->sendMessage($user, [
'conversation_id' => 15,
'message' => 'I agree',
'reply_to_message_id' => 42,
]);
// Forward a message to multiple conversations
$chat->sendMessage($user, [
'conversation_id' => 20,
'message' => $original->message,
'message_type' => $original->message_type,
'forward_to_message_id' => $original->id,
]);
// Edit a message
$updated = $chat->updateMessage($user, ['message' => 'Corrected text'], $message);
// Get messages (paginated, supports search)
$messages = $chat->getMessages($user, $conversationId, query: null, perPage: 20);
// Get pinned messages
$pinned = $chat->pinnedMessages($user, $conversationId);
// Pin or unpin
$chat->pinToggleMessage($user, $message);
// Delete for current user only
$chat->deleteForMe($user, ['message_ids' => [10, 11, 12]]);
// Unsend for everyone
$chat->deleteForEveryone($user, ['message_ids' => [10]]);
// Mark all messages as seen when opening a conversation
$chat->markConversationAsRead($user, $conversationId);
// Mark specific messages as seen (when conversation is already open)
$chat->markMessagesAsRead($user, [
'conversation_id' => 15,
'message_ids' => [40, 41, 42],
]);
// Mark as delivered
$chat->markDelivered($user, $conversationId);
// Toggle a reaction (adds if not present, removes if already reacted with same emoji)
$reactions = $chat->toggleReaction($user, $messageId, '❤️');
// List all reactions grouped by emoji
$reactions = $chat->listReactions($messageId);
// Returns:
// [
// 'total_reactions' => 5,
// 'grouped' => [
// '❤️' => ['count' => 3, 'users' => [...]],
// '👍' => ['count' => 2, 'users' => [...]],
// ]
// ]
// routes/api.php
Route::prefix('api/v1')->middleware(['api', 'auth:sanctum', 'talkbridge.last-seen'])->group(function () {
Route::apiResource('messages', \App\Http\Controllers\Chat\MessageController::class)
->only(['store', 'show', 'update']);
// ... rest of routes from stubs/talkbridge/
});
namespace App\Services;
use RahatulRabbi\TalkBridge\Services\ChatService;
class AppChatService extends ChatService
{
public function sendMessage($user, array $data)
{
// Custom validation
if (strlen($data['message'] ?? '') > 5000) {
throw new \InvalidArgumentException('Message too long.');
}
$message = parent::sendMessage($user, $data);
// Log to your own analytics
// Analytics::track('message_sent', ['user_id' => $user->id]);
return $message;
}
}
// In your EventServiceProvider or AppServiceProvider
use RahatulRabbi\TalkBridge\Events\MessageEvent;
use RahatulRabbi\TalkBridge\Events\ConversationEvent;
Event::listen(MessageEvent::class, function (MessageEvent $event) {
if ($event->type === 'sent') {
// e.g. send email digest, update analytics
}
});
Event::listen(ConversationEvent::class, function (ConversationEvent $event) {
if ($event->action === 'member_added') {
// e.g. send welcome message
}
});
namespace App\Http\Resources\Chat;
use RahatulRabbi\TalkBridge\Http\Resources\Chat\MessageResource as BaseResource;
class MessageResource extends BaseResource
{
public function toArray($request): array
{
$base = parent::toArray($request);
// Add your custom fields
$base['is_bookmarked'] = $request->user()?->bookmarks()->where('message_id', $this->id)->exists();
$base['custom_meta'] = $this->custom_field ?? null;
return $base;
}
}
// Upload a file to the configured disk
$path = talkbridge_upload_file($file, 'uploads/custom');
// Delete a file
talkbridge_delete_file($path);
// Delete multiple files
talkbridge_delete_files([$path1, $path2]);
// Detect file type from extension
$type = talkbridge_file_type('photo.jpg'); // 'image'
// Get user display name (respects composite name config)
$name = talkbridge_user_name($user);
// Get user avatar
$avatar = talkbridge_user_avatar($user);
// Check if user is online
$user->isOnline(); // bool
// Get display name (handles composite columns)
$user->getChatDisplayName(); // string
// Get avatar URL
$user->getChatAvatar(); // string|null
// Get last seen as human diff
$user->getChatLastSeen(); // '2 minutes ago'
// Blocking
$user->hasBlocked($otherUser); // bool
$user->isBlockedBy($otherUser); // bool
$user->blockedUsers(); // BelongsToMany
$user->blockedByUsers(); // BelongsToMany
// Restricting
$user->hasRestricted($otherUser); // bool
$user->restrictedUsers(); // BelongsToMany
$user->restrictedByUsers(); // BelongsToMany
// Device tokens
$user->deviceTokens(); // HasMany
bash
php artisan talkbridge:publish --tag=stubs
bash
php artisan talkbridge:publish --tag=stubs
bash
php artisan talkbridge:uninstall
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.