All Projects → musonza → Chat

musonza / Chat

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

Projects that are alternatives of or similar to Chat

Chatify
A Laravel package that allows you to add a complete user messaging system into your new/existing Laravel application.
Stars: ✭ 885 (+24.65%)
Mutual labels:  laravel, laravel-package, chat
Laravel 5 Messenger
A Simple Laravel 5, 6, 7 & 8 Messenger with Pusher Capabilities
Stars: ✭ 75 (-89.44%)
Mutual labels:  conversation, laravel, chat
Mercurius
Real-time Messenger for Laravel
Stars: ✭ 309 (-56.48%)
Mutual labels:  laravel, laravel-package, chat
Sweet Alert
A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAVASCRIPT'S POPUP BOXES FOR LARAVEL
Stars: ✭ 696 (-1.97%)
Mutual labels:  laravel, laravel-package
Laravel Heyman
Declarative style of authorization and validation in laravel.
Stars: ✭ 677 (-4.65%)
Mutual labels:  laravel, laravel-package
Sharp
Laravel 6+ Content management framework
Stars: ✭ 430 (-39.44%)
Mutual labels:  laravel, laravel-package
Bagisto
An easy to use, free and open source laravel eCommerce platform to build your online shop in no time.
Stars: ✭ 4,140 (+483.1%)
Mutual labels:  laravel, laravel-package
Laravel Open Source Projects
A Web Artisan list of categorized OPEN SOURCE PROJECTS built with Laravel PHP Framework.
Stars: ✭ 676 (-4.79%)
Mutual labels:  laravel, laravel-package
Telegram
✈️ Telegram Notifications Channel for Laravel
Stars: ✭ 450 (-36.62%)
Mutual labels:  laravel, laravel-package
Framework
The truly Laravel E-commerce Framework
Stars: ✭ 456 (-35.77%)
Mutual labels:  laravel, laravel-package
Snooze
A package to simplify automating future notifications and reminders in Laravel
Stars: ✭ 515 (-27.46%)
Mutual labels:  laravel, laravel-package
Laravel Server Monitor
Server Monitoring Command for Laravel Applications
Stars: ✭ 424 (-40.28%)
Mutual labels:  laravel, laravel-package
Eloquent Viewable
Associate views with Eloquent models in Laravel
Stars: ✭ 404 (-43.1%)
Mutual labels:  laravel, laravel-package
Statamic
Statamic 3: The New Site/App Package
Stars: ✭ 431 (-39.3%)
Mutual labels:  laravel, laravel-package
Secure Headers
PHP Secure Headers
Stars: ✭ 379 (-46.62%)
Mutual labels:  laravel, laravel-package
Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (-36.48%)
Mutual labels:  laravel, laravel-package
Laravel Decomposer
⚙️ A Laravel package to decompose your installed packages, their dependencies, your app & server environment
Stars: ✭ 488 (-31.27%)
Mutual labels:  laravel, laravel-package
Laravel Visits
📊 Laravel Visits is a counter that can be attached to any model to track its visits using Redis or Eloquent. (with tags, IP protection and caching)
Stars: ✭ 582 (-18.03%)
Mutual labels:  laravel, laravel-package
Laravel Shopify
A full-featured Laravel package for aiding in Shopify App development
Stars: ✭ 634 (-10.7%)
Mutual labels:  laravel, laravel-package
Generator
Laravel 5.3+ Scaffold Generator, Support both bootstrap and Semantic UI
Stars: ✭ 327 (-53.94%)
Mutual labels:  laravel, laravel-package

chat

Build Status Downloads Packagist

Chat

Create a Chat application for your multiple Models

Table of Contents

Click to expand

Checkout a simple Demo Application

Introduction

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

Installation

From the command line, run:

composer require musonza/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.

Configuration

See musonza_chat.php for configuration

Run the migrations:

php artisan migrate

Usage

You can mix Models as participants. For instance you can have Parents, Students and Professors models communicating

Adding the ability to participate to a Model

Add the Musonza\Chat\Traits\Messageable trait to any Model you want to participate in Conversations For example, let's say we want out Bot model to chat with other Models:

use Illuminate\Database\Eloquent\Model;
use Musonza\Chat\Traits\Messageable;

class Bot extends Model
{
    use Messageable;
}

Get participant details

Since we allow Models with data that differ in structure to chat, we may want a uniform way to represent the participant details in a uniform way.

You can get the details as follows:

$participantModel->getParticipantDetails();

Assuming you have a column name for your model, this returns a default array ['name' => 'column_value'] You can however, customize this for your needs by adding an Eloquent Accessor that returns an array with as much as you need to your model as follows:

    public function getParticipantDetailsAttribute()
    {
        return [
            'name' => $this->someValue,
            'foo' => 'bar',
        ];
    }

Creating a conversation

You can start a conversation by passing an array of Models as participants

$participants = [$model1, $model2,..., $modelN];

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

Creating a conversation of type private / public

You may want to classify conversations as private or public

$participants = [$model1, $model2,..., $modelN];

// Create a private conversation
$conversation = Chat::createConversation($participants)->makePrivate();

// Create a public conversation
$conversation = Chat::createConversation($participants)->makePrivate(false);

// Create a direct message

// Make direct conversation after creation
$conversation = Chat::createConversation($participants)->makeDirect();

// Specify intent for direct conversation before creation
$conversation = Chat::makeDirect()->createConversation($participants);

Note: You will not be able to add additional participants to a direct conversation. Additionally you can't remove a participant from a direct conversation.

Get a conversation by id

$conversation = Chat::conversations()->getById($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($model)
            ->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($model)
		->to($conversation)
		->send();

Get a message by id

$message = Chat::messages()->getById($id);

Get message sender

$sendModel = $message->sender;

Mark a message as read

Chat::message($message)->setParticipant($participantModel)->markRead();

Flag / mark a message

Chat::message($message)->setParticipant($participantModel)->toggleFlag();

Chat::message($message)->setParticipant($participantModel)->flagged(); // true

Mark whole conversation as read

Chat::conversation($conversation)->setParticipant($participantModel)->readAll();

Unread messages count

$unreadCount = Chat::messages()->setParticipant($participantModel)->unreadCount();

Unread messages count per Conversation

Chat::conversation($conversation)->setParticipant($participantModel)->unreadCount();

Delete a message

Chat::message($message)->setParticipant($participantModel)->delete();

Cleanup Deleted Messages

What to cleanup when all participants have deleted a $message or $conversation?

Listen for \Musonza\Chat\Eventing\AllParticipantsDeletedMessage and

\Musonza\Chat\Eventing\AllParticipantsClearedConversation

Clear a conversation

Chat::conversation($conversation)->setParticipant($participantModel)->clear();

Get participant conversations

Chat::conversations()->setPaginationParams(['sorting' => 'desc'])
	->setParticipant($participantModel)
	->limit(1)
	->page(1)
	->get();

Get a conversation between two participants

$conversation = Chat::conversations()->between($participantModel1, $participantModel2);

Get common conversations among participants

$conversations = Chat::conversations()->common($participants);

$participants is an array of participant Models

Remove participants from a conversation

/* removing one user */
Chat::conversation($conversation)->removeParticipants([$participantModel]);
/* removing multiple participants */
Chat::conversation($conversation)->removeParticipants([$participantModel, $participantModel2,...,$participantModelN]);

Add participants to a conversation

/* add one user */
Chat::conversation($conversation)->addParticipants([$participantModel]);
/* add multiple participants */
Chat::conversation($conversation)->addParticipants([$participantModel, $participantModel2]);

Get messages in a conversation

Chat::conversation($conversation)->setParticipant($participantModel)->getMessages()

Get user conversations by type

// private conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->isPrivate()->get();

// public conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->isPrivate(false)->get();

// direct conversations / messages
$conversations = Chat::conversations()->setParticipant($participantModel)->isDirect()->get();

// all conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->get();

Get recent messages

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

Pagination

There are a few ways you can achieve pagination You can specify the limit and page as above using the respective functions or as below:

   $paginated = Chat::conversations()->setParticipant($participant)
            ->setPaginationParams([
                'page' => 3,
                'perPage' => 10,
                'sorting' => "desc",
                'columns' => [
                    '*'
                ],
                'pageName' => 'test'
            ])
            ->get();

You don't have to specify all the parameters. If you leave the parameters out, default values will be used. $paginated above will return Illuminate\Pagination\LengthAwarePaginator To get the conversations simply call $paginated->items()

Get participants in a conversation

$participants = $conversation->getParticipants();

Get participation entry for a Model in a conversation

Chat::conversation($conversation)->getParticipation($model);

Update participation settings

Set Conversation settings for participant (example: mute_mentions, mute_conversation)

$settings = ['mute_mentions' => true];

Chat::conversation($conversation)
    ->getParticipation($this->alpha)
    ->update(['settings' => $settings]);

Data Transformers

Need to have more control on the data returned from the package routes? You can specify your own Model transformers and take advantage of Fractal.

All you need to do is specify the location of your transformers in the configuration file musonza_chat.php as follows:

/**
 * Model Transformers
 */
'transformers' => [
    'conversation' => \MyApp\Transformers\ConversationTransformer::class,
    'message' => \MyApp\Transformers\MessageTransformer::class,
    'participant' => \MyApp\Transformers\ParticipantTransformer::class,
]

Note: This only applies to responses from package routes.

License

Chat is open-sourced software licensed under the MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].