Download the PHP package musonza/chat without Composer
On this page you can find all versions of the php package musonza/chat. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package chat
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
- Creating a named 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
- Filter messages by type
- Get messages with cursor pagination
- Filter conversations by name
- Get public conversations for discovery
- Get recent messages
- Get participants in a conversation
- Get all conversation partners
- 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:
Publish the assets:
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:
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:
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:
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:
Creating a conversation
You can start a conversation by passing an array of Models as participants
Creating a conversation of type private / public
You may want to classify conversations as private or public
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.
Creating a named conversation
You can assign a name to a conversation, which is useful for chatrooms or any scenario where you need to look up a conversation by a human-readable identifier.
The name column is nullable, so existing conversations and conversations created without a name are unaffected.
Get a conversation by id
Update conversation details
Send a text message
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:
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.
Get a message by id
Get message sender
Mark a message as read
Flag / mark a message
Mark whole conversation as read
Unread messages count
Unread messages count per Conversation
Delete a message
Message Reactions
Add emoji or text-based reactions to messages:
Remove a reaction:
Toggle a reaction (add if not present, remove if present):
Get reactions summary with counts:
Check if participant has reacted:
Get all reactions on a message:
You can also access reactions directly on the Message model:
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 conversation
Archive a conversation (per participant)
Archiving is per-participant — like Gmail/Outlook — so one user can archive a thread without hiding it for the others. By default, archived conversations are excluded from a participant's listings. A new incoming message auto-unarchives the recipient (configurable).
To disable auto-unarchive on incoming messages, set unarchive_on_new_message => false in config/musonza_chat.php.
Get participant conversations
Get a conversation between two participants
Get common conversations among participants
$participants is an array of participant Models
Remove participants from a conversation
Add participants to a conversation
Get messages in a conversation
Filter messages by type
You can filter messages by their type (e.g., text, image, attachment) using the ofType method.
Get messages with cursor pagination
For real-time chat applications, cursor-based pagination is recommended over offset-based pagination. This prevents duplicate messages when new messages arrive between page loads.
API Endpoint: GET /conversations/{id}/messages-cursor
Query parameters:
participant_id(required)participant_type(required)perPage(optional, default: 25)sorting(optional:ascordesc)cursor(optional - from previous response'snext_cursor)
The response includes next_cursor and prev_cursor for navigation.
Get user conversations by type
Filter conversations by name
Get public conversations for discovery
You can list public conversations without being a participant. This is useful for building discovery pages where users can browse and join public chat rooms (similar to Telegram public groups).
Note: This only works for public conversations. Attempting to list private conversations without setting a participant will throw an
InvalidConversationListException.
Get recent messages
Pagination
There are a few ways you can achieve pagination
You can specify the limit and page as above using the respective functions or as below:
You don't have to specify all the parameters. If you leave the parameters out, default values will be used.
$paginated above will return Illuminate\Pagination\LengthAwarePaginator
To get the conversations simply call $paginated->items()
Tip: For paginating messages in real-time chat applications, consider using cursor pagination instead. Cursor pagination prevents duplicate messages when new messages arrive between page loads.
Get participants in a conversation
Get all conversation partners
Get all unique models that a user has ever been in a conversation with:
This returns a collection of unique models, excluding the user themselves. If the same partner appears in multiple conversations, they will only be included once.
Get participation entry for a Model in a conversation
Update participation settings
Set Conversation settings for participant (example: mute_mentions, mute_conversation)
Data Transformers
Need to have more control on the data returned from the package routes? You can specify your own Model transformers and take advantage of Fractal.
All you need to do is specify the location of your transformers in the configuration
file musonza_chat.php as follows:
Note: This only applies to responses from package routes.
Message Encryption
You can optionally encrypt message bodies at rest using Laravel's built-in encryption. When enabled, messages are encrypted using AES-256-CBC via Laravel's Crypt facade.
To enable encryption, set the encrypt_messages option in your musonza_chat.php config file:
How it works:
- New messages are automatically encrypted before being stored in the database
- Messages are automatically decrypted when retrieved via the model
- Existing unencrypted messages remain readable (hybrid mode)
- An
is_encryptedcolumn tracks whether each message is encrypted
Note: Encryption uses your application's
APP_KEY. If you change yourAPP_KEY, previously encrypted messages will become unreadable.
License
Chat is open-sourced software licensed under the MIT license
All versions of chat with dependencies
illuminate/contracts Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0
spatie/laravel-fractal Version ^6.0|dev-main