Git Product home page Git Product logo

php-laravel-chat-api's Introduction

PHP Unit PHP CodeSniffer codecov

Laravel Chat Api

Adds chat functionality for your project on top of Laravel Broadcasting feature.

Laravel 5.5+

Lower versions are not supported.

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.

Configure at least one broadcasting driver

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
 $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
 $chatService->leaveChat($user, $chat);  

When creator leaves chat on of participants became to creator.

  • Send message in chat
 $chatService->sendMessage($sender, $chat, $message);  
  • Mark chat as read
 $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:

...
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.

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:

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

php-laravel-chat-api's People

Contributors

hollow-en avatar maxermolenko avatar populov avatar veeeeerysexy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-laravel-chat-api's Issues

Laravel version

Hi, just wonderng why Laravel 54 is not supported in v2.0 ? Is it just because of package auto discovery ?

Service Provider not found

[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'Saritasa\LaravelChatApi\ChatServiceProvider' not found

Laravel Framework 5.5.13

composer.json
"saritasa/laravel-chat-api": "^1.0"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.