Laravel wrapper for Pusher Chatkit. Find out more about Chatkit here.
Note: This package requires Laravel 5.5 or above
composer require shalvah/pusher-chatkit-laravel
The package will automatically make use of the latest stable version of the Chatkit PHP library (currently 1.1.0).
Publish the config file by running:
php artisan vendor:publish --provider="Chatkit\Laravel\ChatkitServiceProvider"
This will create a config/chatkit.php
file in your app that you can modify to match your configuration.
Retrieve your Chatkit app details from your Chatkit app dashboard and add them in your .env
file like so:
CHATKIT_INSTANCE_LOCATOR=your-instance-locator
CHATKIT_KEY=your-key
That's it. You can use Chatkit via the facade in your app:
<?php
use Chatkit\Laravel\Facades\Chatkit;
public function startChatting()
{
Chatkit::createUser(['id' => 'hc', 'name' => 'Hamilton Chapman']);
Chatkit::createRoom(['creator_id' => 'hc', 'name' => 'Cat Lovers']);
Chatkit::sendMessage(['sender_id' => 'hc', 'room_id' => 'r001', 'text' => 'Hi, everyone!' ]);
}
Alternatively, you may inject the ChatkitManager
into your methods:
<?php
use Chatkit\Laravel\ChatkitManager;
public function startChatting(ChatkitManager $chatkitManager)
{
$chatkitManager->createUser(['id' => 'hc', 'name' => 'Hamilton Chapman']);
$chatkitManager->createRoom(['creator_id' => 'hc', 'name' => 'Cat Lovers']);
$chatkitManager->sendMessage([
'sender_id' => 'hc',
'room_id' => 'r001',
'text' => 'Hi, everyone!'
]);
}
The config/chatkit.php
file allows you to configure your Chatkit usage (for instance, to use multiple connections).
Supposing you have to work with multiple chat apps from the same server. You can do this easily by publishing the config file as explained above, then configuring your various connections in it as needed. You can then switch connections as needed using either the facade or Manager class:
<?php
// use whatever connection is default -- by default, this is 'main'
Chatkit::createRoom(['creator_id' =>'admin', 'name' => 'Just Chat']);
// use the 'main' connection
Chatkit::connection('main')->createRoom(['creator_id' =>'admin', 'name' => 'Just Chat']);
// use the 'test' connection
Chatkit::connection('test')->createRoom('admin', ['name' => 'Just Chat']);
// use the 'secondary' connection
Chatkit::setDefaultConnection('secondary');
Chatkit::createRoom(['creator_id' =>'admin', 'name' => 'Just Chat']);
A complete listing of available methods is available at the Chatkit PHP SDK docs.
MIT