, (*1)
Laravel Chat Api
Adds chat functionality for your project on top of Laravel Broadcasting feature., (*2)
Laravel 5.5+
Lower versions are not supported., (*3)
Installation and configuration Install the saritasa/laravel-chat-api
package:
bash $ composer require saritasa/laravel-chat-api
Publish config with
bash $ artisan vendor:publish --tag=laravel_chat_api
Update config/laravel_chat_api.php sections:
- Implement IChatUser contract to your application user model and update parameter userModelClass
.
- Check notifications section and add your own notification instead this mocks., (*4)
Configure at least one broadcasting driver, (*5)
Work with service Add IChatService contract injection in needed class.
Methods: - Create chat
$chatService->createChat($creator, ['name' => 'New Chat'], [1, 2]);
```
Where [1, 2] - identifiers of participants of chat excluded creator.
- Close chat
```php
$chatService->closeChat($creator, $chat);
```
Remember that only creator can close chat and can't close "already closed" chat. In this cases ChatException will be
thrown.
- Leave chat
```php
$chatService->leaveChat($user, $chat);
```
When creator leaves chat on of participants became to creator.
- Send message in chat
```php
$chatService->sendMessage($sender, $chat, $message);
```
- Mark chat as read
```php
$chatService->markChatAsRead($chat, $user);
```
### Events:
## Chat events:
Throws in chat channel for all subscribers who participate in chatting.
- MessageSentEvent throws when participant sent new message in chat.
- ChatLeavedEvent throws when one of participants leaved this chat.
- ChatClosedEvent throws when creator closed this chat.
## User events:
Throws in user channel for all subscribers who participate in chatting exclude event initiator.
- ChatCreatedEvent throws when user created new chat.
- MessageSentEvent throws when participant sent new message in chat.
- ChatClosedUserEvent throws when creator closed this chat.
- ChatReopenedUserEvent throws when creator reopened closed chat.
## Broadcasting settings:
To integrate it with Laravel broadcasting you can just update your broadcasting routes file ( routes/channels.php by default ) with next example:
```php
...
use Saritasa\LaravelChatApi\Events\ChatCreatedEvent;
use Saritasa\LaravelChatApi\Events\ChatEvent;
use Saritasa\LaravelChatApi\Contracts\IChatUser;
...
// Using model binding for chat events channel.
Broadcast::channel(ChatEvent::CHANNEL_PREFIX . '{chat}', function ($user, Chat $chat) {
// Checking that auth user is chat participant.
return $chat->getUsers()->pluck('id')->contains($user->id);
});
Broadcast::channel(ChatCreatedEvent::CHANNEL_PREFIX . '{id}', function ($user, int $id) {
return (int)$user->id === $id;
});
Read in official documentation about defining authorization callbacks.
Now all chat events will dispatch through your broadcast driver., (*6)
Notifications settings:
To easy sent notifications you should create your own notification and update laravel_chat_api.php
config file.
Here is example if we should send notification when chat was reopened:, (*7)
return [
...
'notifications' => [
...
'chatReopened' => App\Notifications\MyOwnChatReopenedNotification::class,
],
];
Contributing
- Create fork
- Checkout fork
- Develop locally as usual. Code must follow PSR-1, PSR-2
- Update README.md to describe new or changed functionality. Add changes description to CHANGE file.
- When ready, create pull request
Resources