Skip to content
forked from musonza/chat

A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

License

Notifications You must be signed in to change notification settings

SingletonIT/chat

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Downloads Packagist

Chat

Introduction

This package allows you to add a chat system to your Laravel ^5.4 application

Note: If you are using a Laravel version less than 5.4 install the release on this branch instead.

Installation

From the command line, run:

composer require musonza/chat

Add the service provider to your config\app.php the providers array

Musonza\Chat\ChatServiceProvider

Add the Facade to your aliases:

'Chat' => Musonza\Chat\Facades\ChatFacade::class to your `config\app.php`

The class is bound to the ioC as chat

$chat = App::make('chat');

Publish the assets:

php artisan vendor:publish

This will publish database migrations and a configuration file musonza_chat.php in the Laravel config folder.

Note: This package takes advantage of Laravel Notifications. If you have already setup Laravel notifications you can delete the 2017_07_12_034227_create_notifications_table.php migration file.

Configuration

[
    'user_model'            => 'App\User',

    /**
     * This will allow you to broadcast an event when a message is sent
     * Example:
     * Channel: private-mc-chat-conversation.2, 
     * Event: Musonza\Chat\Messages\MessageWasSent 
     */
    'broadcasts'            => false,

    /**
     * If set to true, this will use Laravel notifications table to store each
     * user message notification.
     * Otherwise it will use mc_message_notification table.
     * If your database doesn't support JSON columns you will need to set this to false.
     */
    'laravel_notifications' => true,
];

Run the migrations:

php artisan migrate

Usage

By default the package assumes you have a User model in the App namespace.

However, you can update the user model in musonza_chat.php published in the config folder.

Creating a conversation

$participants = [$userId, $userId2,...];

$conversation = Chat::createConversation($participants); 

Get a conversation by id

$conversation = Chat::conversation($conversation_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($user)
            ->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($user)
		->to($conversation)
		->send(); 

Get a message by id

$message = Chat::messageById($id);

Mark a message as read

Chat::messages($message)->for($user)->markRead();

Mark whole conversation as read

Chat::conversations($conversation)->for($user)->readAll();

Unread messages count

$unreadCount = Chat::for($user)->unreadCount();

Delete a message

Chat::messages($message)->for($user)->delete();

Clear a conversation

Chat::conversations($conversation)->for($user)->clear();

Get a conversation between two users

Chat::getConversationBetween($user1, $user2);

Get common conversations among users

$conversations = Chat::commonConversations($users);

$users can be an array of user ids ex. [1,4,6] or a collection (\Illuminate\Database\Eloquent\Collection) of users

Remove users from a conversation

/* removing one user */
Chat::removeParticipants($conversation, $user);
/* removing multiple users */
Chat::removeParticipants($conversation, [$user1, $user2, $user3,...,$userN]);

Add users to a conversation

/* add one user */
Chat::addParticipants($conversation, $user); 
/* add multiple users */
Chat::addParticipants($conversation, [$user3, $user4]);

Note: A third user will classify the conversation as not private if it was.

Get messages in a conversation

Chat::conversations($conversation)->for($user)->getMessages($perPage, $page)

Get recent messages

$messages = Chat::conversations()->for($user)->limit(25)->page(1)->get();

Get users in a conversation

$users = $conversation->users;

License

Chat is open-sourced software licensed under the MIT license

About

A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%