Chat
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.
Install / Use
/learn @musonza/ChatREADME
Chat
Create a Chat application for your multiple Models
What to learn how to make a package like this? https://leanpub.com/laravel-package-development
Table of Contents
- Introduction
- Installation
- Usage
- Adding the ability to participate to a Model
- Enable the routes
- Get participant details
- Creating a conversation
- Get a conversation by Id
- Update conversation details
- Send a text message
- Send a message of custom type
- Get a message by id
- Get message sender
- Mark a message as read
- Mark whole conversation as read
- Unread messages count
- Delete a message
- Message Reactions
- Cleanup Deleted Messages
- Clear a conversation
- Get participant conversations
- Get a conversation between two participants
- Get common conversations among participants
- Remove participants from a conversation
- Add participants to a conversation
- Get messages in a conversation
- Get messages with cursor pagination
- Get public conversations for discovery
- Get recent messages
- Get participants in a conversation
- Get participation entry for a Model in a conversation
- Update participation settings
- Data Transformers
- Message Encryption
- License
Checkout a simple Demo Application
Introduction
This package allows you to add a chat system to your Laravel ^5.4 application
Installation
From the command line, run:
composer require musonza/chat
Publish the assets:
php artisan vendor:publish --provider="Musonza\Chat\ChatServiceProvider"
This will publish database migrations and a configuration file musonza_chat.php in the Laravel config folder.
Configuration
See musonza_chat.php for configuration
Run the migrations:
php artisan migrate
Usage
You can mix Models as participants. For instance you can have Parents, Students and Professors models communicating
Adding the ability to participate to a Model
Add the Musonza\Chat\Traits\Messageable trait to any Model you want to participate in Conversations
For example, let's say we want out Bot model to chat with other Models:
use Illuminate\Database\Eloquent\Model;
use Musonza\Chat\Traits\Messageable;
class Bot extends Model
{
use Messageable;
}
Enable the routes
The package includes routes for conversations, conversation participants, and messaging. These routes are hidden by default. To enable them, change should_load_routes to true, but make sure to configure the middleware correctly.
Get participant details
Since we allow Models with data that differ in structure to chat, we may want a uniform way to represent the participant details in a uniform way.
You can get the details as follows:
$participantModel->getParticipantDetails();
Assuming you have a column name for your model, this returns a default array ['name' => 'column_value']
You can however, customize this for your needs by adding an Eloquent Accessor that returns an array
with as much as you need to your model as follows:
public function getParticipantDetailsAttribute()
{
return [
'name' => $this->someValue,
'foo' => 'bar',
];
}
Creating a conversation
You can start a conversation by passing an array of Models as participants
$participants = [$model1, $model2,..., $modelN];
$conversation = Chat::createConversation($participants);
Creating a conversation of type private / public
You may want to classify conversations as private or public
$participants = [$model1, $model2,..., $modelN];
// Create a private conversation
$conversation = Chat::createConversation($participants)->makePrivate();
// Create a public conversation
$conversation = Chat::createConversation($participants)->makePrivate(false);
// Create a direct message
// Make direct conversation after creation
$conversation = Chat::createConversation($participants)->makeDirect();
// Specify intent for direct conversation before creation
$conversation = Chat::makeDirect()->createConversation($participants);
Note: You will not be able to add additional participants to a direct conversation. Additionally you can't remove a participant from a direct conversation.
Get a conversation by id
$conversation = Chat::conversations()->getById($id);
Update conversation details
$data = ['title' => 'PHP Channel', 'description' => 'PHP Channel Description'];
$conversation->update(['data' => $data]);
Send a text message
$message = Chat::message('Hello')
->from($model)
->to($conversation)
->send();
Send a message of custom type
The default message type is text. If you want to specify custom type you can call the type() function as below:
$message = Chat::message('http://example.com/img')
->type('image')
->from($model)
->to($conversation)
->send();
To add more details about a message
Sometimes you might want to add details about a message. For example, when the message type is an attachment, and you want to add details such as attachment's filename, and attachment's file url, you can call the data() function and pass your data as an array.
$message = Chat::message('Attachment 1')
->type('attachment')
->data(['file_name' => 'post_image.jpg', 'file_url' => 'http://example.com/post_img.jpg'])
->from($model)
->to($conversation)
->send();
Get a message by id
$message = Chat::messages()->getById($id);
Get message sender
$sendModel = $message->sender;
Mark a message as read
Chat::message($message)->setParticipant($participantModel)->markRead();
Flag / mark a message
Chat::message($message)->setParticipant($participantModel)->toggleFlag();
Chat::message($message)->setParticipant($participantModel)->flagged(); // true
Mark whole conversation as read
Chat::conversation($conversation)->setParticipant($participantModel)->readAll();
Unread messages count
$unreadCount = Chat::messages()->setParticipant($participantModel)->unreadCount();
Unread messages count per Conversation
Chat::conversation($conversation)->setParticipant($participantModel)->unreadCount();
Delete a message
Chat::message($message)->setParticipant($participantModel)->delete();
Message Reactions
Add emoji or text-based reactions to messages:
// Add a reaction
Chat::message($message)->setParticipant($participantModel)->react('👍');
// Add multiple different reactions
Chat::message($message)->setParticipant($participantModel)->react('❤️');
Remove a reaction:
Chat::message($message)->setParticipant($participantModel)->unreact('👍');
Toggle a reaction (add if not present, remove if present):
$result = Chat::message($message)->setParticipant($participantModel)->toggleReaction('👍');
// $result = ['added' => true/false, 'reaction' => Reaction|null]
Get reactions summary with counts:
$summary = Chat::message($message)->reactionsSummary();
// ['👍' => 5, '❤️' => 3, '😂' => 1]
Check if participant has reacted:
// Check for specific reaction
Chat::message($message)->setParticipant($participantModel)->hasReacted('👍');
// Check for any reaction
Chat::message($message)->setParticipant($participantModel)->hasReacted();
Get all reactions on a message:
$reactions = Chat::message($message)->reactions();
You can also access reactions directly on the Message model:
$message->reactions; // All reactions
$message->getReactionsSummary(); // Grouped counts
$message->react($participant, '👍'); // Add
$message->unreact($participant, '👍'); // Remove
$message->hasReacted($participant, '👍'); // Check
Broadcasting: When broadcasting is enabled, MessageReactionAdded and MessageReactionRemoved events are broadcast to the conversation channel.
Cleanup Deleted Messages
What to cleanup when all participants have deleted a $message or $conversation?
Listen for \Musonza\Chat\Eventing\AllParticipantsDeletedMessage and
\Musonza\Chat\Eventing\AllParticipantsClearedConversation
Clear a convers
Related Skills
node-connect
352.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
