2017 © Pedro Peláez
 

library laravel-chat-api

Classes, that implement chat API on top of laravel

image

saritasa/laravel-chat-api

Classes, that implement chat API on top of laravel

  • Saturday, July 28, 2018
  • by saritasa
  • Repository
  • 4 Watchers
  • 0 Stars
  • 37 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 3 Versions
  • 311 % Grown

The README.md

PHP Unit PHP CodeSniffer codecov, (*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

  1. Create fork
  2. Checkout fork
  3. Develop locally as usual. Code must follow PSR-1, PSR-2
  4. Update README.md to describe new or changed functionality. Add changes description to CHANGE file.
  5. When ready, create pull request

Resources

The Versions

28/07 2018

dev-master

9999999-dev

Classes, that implement chat API on top of laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sergey Populov

laravel api chat

22/06 2018

2.0.0

2.0.0.0

Classes, that implement chat API on top of laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sergey Populov

laravel api chat

11/04 2017

1.0.0

1.0.0.0

Classes, that implement chat API on top of laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sergey Populov

laravel api chat