Giter Site home page Giter Site logo

chat's Introduction

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

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.