├── .gitattributes ├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── config └── chatkit.php ├── phpunit.xml.dist └── src ├── ChatkitFactory.php ├── ChatkitManager.php ├── ChatkitServiceProvider.php └── Facades └── Chatkit.php /.gitattributes: -------------------------------------------------------------------------------- 1 | tests/ export-ignore 2 | pusher.png export-ignore 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | phpunit.xml 3 | vendor 4 | .idea/ 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.0 5 | - 7.1 6 | - 7.2 7 | - 7.3 8 | - 7.4 9 | 10 | sudo: false 11 | 12 | install: 13 | - travis_retry composer install --no-interaction 14 | 15 | script: 16 | - vendor/bin/phpunit 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-Chatkit 2 | 3 | _ChatKit is shutting down - https://blog.pusher.com/narrowing-our-product-focus_ 4 | 5 |

6 | Pusher 7 |

8 | 9 | > Laravel wrapper for [Pusher Chatkit](https://github.com/pusher/chatkit-server-php). Find out more about Chatkit [here](https://pusher.com/chatkit). 10 | 11 | [![Build Status](https://img.shields.io/travis/shalvah/pusher-chatkit-laravel/master.svg?style=flat)](https://travis-ci.org/shalvah/pusher-chatkit-laravel) 12 | [![Latest Stable Version](https://poser.pugx.org/shalvah/pusher-chatkit-laravel/v/stable)](https://packagist.org/packages/shalvah/pusher-chatkit-laravel) [![Total Downloads](https://poser.pugx.org/shalvah/pusher-chatkit-laravel/downloads)](https://packagist.org/packages/shalvah/pusher-chatkit-laravel) 13 | [![Latest Version](https://img.shields.io/github/release/shalvah/pusher-chatkit-laravel.svg?style=flat)](https://github.com/shalvah/pusher-chatkit-laravel/releases) 14 | 15 | *Note*: This package requires Laravel 5.5 or above 16 | 17 | ## Installation 18 | 19 | ```bash 20 | composer require shalvah/pusher-chatkit-laravel 21 | ``` 22 | The package will automatically make use of the latest stable version of the Chatkit PHP library (currently 1.1.0). 23 | 24 | ## Quick start 25 | Publish the config file by running: 26 | 27 | ```bash 28 | php artisan vendor:publish --provider="Chatkit\Laravel\ChatkitServiceProvider" 29 | ``` 30 | 31 | This will create a `config/chatkit.php` file in your app that you can modify to match your configuration. 32 | Retrieve your Chatkit app details from your Chatkit app dashboard and add them in your `.env` file like so: 33 | 34 | ```bash 35 | CHATKIT_INSTANCE_LOCATOR=your-instance-locator 36 | CHATKIT_KEY=your-key 37 | ``` 38 | 39 | That's it. You can use Chatkit via the facade in your app: 40 | 41 | ```php 42 | 'hc', 'name' => 'Hamilton Chapman']); 49 | Chatkit::createRoom(['creator_id' => 'hc', 'name' => 'Cat Lovers']); 50 | Chatkit::sendMessage(['sender_id' => 'hc', 'room_id' => 'r001', 'text' => 'Hi, everyone!' ]); 51 | } 52 | ``` 53 | Alternatively, you may inject the `ChatkitManager` into your methods: 54 | 55 | ```php 56 | createUser(['id' => 'hc', 'name' => 'Hamilton Chapman']); 62 | $chatkitManager->createRoom(['creator_id' => 'hc', 'name' => 'Cat Lovers']); 63 | $chatkitManager->sendMessage([ 64 | 'sender_id' => 'hc', 65 | 'room_id' => 'r001', 66 | 'text' => 'Hi, everyone!' 67 | ]); 68 | } 69 | ```` 70 | 71 | ## Configuration 72 | 73 | The `config/chatkit.php` file allows you to configure your Chatkit usage (for instance, to use multiple connections). 74 | 75 | ## Working with Multiple Connections 76 | 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: 77 | 78 | ```php 79 | 'admin', 'name' => 'Just Chat']); 83 | 84 | // use the 'main' connection 85 | Chatkit::connection('main')->createRoom(['creator_id' =>'admin', 'name' => 'Just Chat']); 86 | 87 | // use the 'test' connection 88 | Chatkit::connection('test')->createRoom('admin', ['name' => 'Just Chat']); 89 | 90 | // use the 'secondary' connection 91 | Chatkit::setDefaultConnection('secondary'); 92 | Chatkit::createRoom(['creator_id' =>'admin', 'name' => 'Just Chat']); 93 | ``` 94 | 95 | ## Documentation 96 | A complete listing of available methods is available at [the Chatkit PHP SDK docs](https://github.com/pusher/chatkit-server-php). 97 | 98 | ## License 99 | 100 | MIT 101 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shalvah/pusher-chatkit-laravel", 3 | "description": "Laravel wrapper for the Chatkit PHP SDK", 4 | "license": "MIT", 5 | "keywords": ["laravel", "pusher", "chatkit", "http", "api", "php-chatkit-server", "rest", "realtime", "chat"], 6 | "authors": [ 7 | { 8 | "name": "Shalvah Adebayo", 9 | "homepage": "https://shalvah.me" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.0", 14 | "illuminate/contracts": "^5.5 || ^6.0 || ^7.0", 15 | "illuminate/support": "^5.5 || ^6.0 || ^7.0", 16 | "graham-campbell/manager": "^3.0 || ^4.0", 17 | "pusher/pusher-chatkit-server": "^1.0.0" 18 | }, 19 | "require-dev": { 20 | "mockery/mockery": "^1.0", 21 | "orchestra/testbench": "^3.5 || ^4.0 || ^5.0", 22 | "phpunit/phpunit": "^6.3" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Chatkit\\Laravel\\": "src/" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "Chatkit\\Laravel\\Tests\\": "tests/" 32 | } 33 | }, 34 | "config": { 35 | "preferred-install": "dist" 36 | }, 37 | "extra": { 38 | "laravel": { 39 | "providers": [ 40 | "Chatkit\\Laravel\\ChatkitServiceProvider" 41 | ], 42 | "aliases": { 43 | "Chatkit": "Chatkit\\Laravel\\Facades\\Chatkit" 44 | } 45 | } 46 | }, 47 | "minimum-stability": "dev", 48 | "prefer-stable": true 49 | } 50 | -------------------------------------------------------------------------------- /config/chatkit.php: -------------------------------------------------------------------------------- 1 | 'main', 28 | 29 | /* 30 | |-------------------------------------------------------------------------- 31 | | Chatkit Connections 32 | |-------------------------------------------------------------------------- 33 | | 34 | | Here are each of the connections setup for your application. Example 35 | | configuration has been included, but you may add as many connections as 36 | | you would like. 37 | | 38 | */ 39 | 40 | 'connections' => [ 41 | 42 | 'main' => [ 43 | /** 44 | * Your Chatkit instance locator. Get this from your Chatkit app dashboard 45 | */ 46 | 'instance_locator' => env('CHATKIT_INSTANCE_LOCATOR'), 47 | 48 | /** 49 | * Your Chatkit instance locator. Get this from your Chatkit app dashboard 50 | */ 51 | 'key' => env('CHATKIT_KEY'), 52 | 53 | 'port' => 80, 54 | 55 | 'timeout' => 30, 56 | 57 | 'debug' => false, 58 | 59 | 'curl_options' => [], 60 | ], 61 | 62 | 'alternative' => [ 63 | 64 | 'instance_locator' => 'your-instance-locator', 65 | 66 | 'key' => 'your-auth-key', 67 | 68 | 'port' => 80, 69 | 70 | 'timeout' => 30, 71 | 72 | 'debug' => false, 73 | 74 | 'curl_options' => [], 75 | ], 76 | 77 | ], 78 | 79 | ]; 80 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | ./tests 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/ChatkitFactory.php: -------------------------------------------------------------------------------- 1 | 15 | */ 16 | class ChatkitFactory 17 | { 18 | protected $configKeys = [ 19 | 'instance_locator', 20 | 'key', 21 | 'port', 22 | 'timeout', 23 | 'debug', 24 | 'curl_options', 25 | ]; 26 | 27 | /** 28 | * Make a new Chatkit client with a given configuration 29 | * 30 | * @param array $config 31 | * 32 | * @return Chatkit 33 | */ 34 | public function make(array $config): Chatkit 35 | { 36 | $config = $this->extractConfig($config); 37 | return $this->createClient($config); 38 | } 39 | 40 | /** 41 | * Extract relevant configuration for creating a new Chatkit instance 42 | * 43 | * @param string[] $config 44 | * 45 | * @throws \InvalidArgumentException 46 | * 47 | * @return array 48 | */ 49 | protected function extractConfig(array $config): array 50 | { 51 | foreach ($this->configKeys as $key) { 52 | if (!array_key_exists($key, $config)) { 53 | throw new InvalidArgumentException("Missing Chatkit configuration key [$key]."); 54 | } 55 | } 56 | 57 | return Arr::only($config, $this->configKeys); 58 | } 59 | 60 | /** 61 | * Instantiate a new Chatkit client. 62 | * 63 | * @param string[] $config 64 | * 65 | * @return Chatkit 66 | */ 67 | protected function createClient(array $config): Chatkit 68 | { 69 | return new Chatkit($config); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/ChatkitManager.php: -------------------------------------------------------------------------------- 1 | 16 | * @mixin Chatkit 17 | */ 18 | class ChatkitManager extends AbstractManager 19 | { 20 | /** 21 | * The factory instance. 22 | * 23 | * @var ChatkitFactory 24 | */ 25 | private $factory; 26 | 27 | /** 28 | * Create a new Chatkit manager instance. 29 | * 30 | * @param \Illuminate\Contracts\Config\Repository $config 31 | * @param ChatkitFactory $factory 32 | * 33 | * @return void 34 | */ 35 | public function __construct(Repository $config, ChatkitFactory $factory) 36 | { 37 | parent::__construct($config); 38 | $this->factory = $factory; 39 | } 40 | 41 | /** 42 | * Create the connection instance. 43 | * 44 | * @param array $config 45 | * 46 | * @return Chatkit 47 | */ 48 | protected function createConnection(array $config): Chatkit 49 | { 50 | return $this->factory->make($config); 51 | } 52 | 53 | /** 54 | * Get the key used to reference this package in app config. 55 | * 56 | * @return string 57 | */ 58 | protected function getConfigName(): string 59 | { 60 | return 'chatkit'; 61 | } 62 | 63 | public function getFactory(): ChatkitFactory 64 | { 65 | return $this->factory; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/ChatkitServiceProvider.php: -------------------------------------------------------------------------------- 1 | 15 | */ 16 | class ChatkitServiceProvider extends ServiceProvider 17 | { 18 | /** 19 | * Boot the service provider. 20 | * 21 | * @return void 22 | */ 23 | public function boot() 24 | { 25 | $this->setupConfig(); 26 | } 27 | 28 | /** 29 | * Setup the config. 30 | * 31 | * @return void 32 | */ 33 | protected function setupConfig() 34 | { 35 | $configFile = realpath(__DIR__.'/../config/chatkit.php'); 36 | $this->publishes([ 37 | $configFile => config_path('chatkit.php') 38 | ]); 39 | } 40 | 41 | /** 42 | * Register the service provider. 43 | * 44 | * @return void 45 | */ 46 | public function register() 47 | { 48 | $this->registerFactory(); 49 | $this->registerManager(); 50 | $this->registerBindings(); 51 | $this->mergeConfigFrom(realpath(__DIR__.'/../config/chatkit.php'), 'chatkit'); 52 | } 53 | 54 | /** 55 | * Register the factory class. 56 | * 57 | * @return void 58 | */ 59 | protected function registerFactory() 60 | { 61 | $this->app->singleton('chatkit.factory', function () { 62 | return new ChatkitFactory(); 63 | }); 64 | 65 | $this->app->alias('chatkit.factory', ChatkitFactory::class); 66 | } 67 | 68 | /** 69 | * Register the manager class. 70 | * 71 | * @return void 72 | */ 73 | protected function registerManager() 74 | { 75 | $this->app->singleton('chatkit', function (Container $app) { 76 | $config = $app['config']; 77 | $factory = $app['chatkit.factory']; 78 | return new ChatkitManager($config, $factory); 79 | }); 80 | 81 | $this->app->alias('chatkit', ChatkitManager::class); 82 | } 83 | 84 | /** 85 | * Register the bindings. 86 | * 87 | * @return void 88 | */ 89 | protected function registerBindings() 90 | { 91 | $this->app->bind('chatkit.connection', function (Container $app) { 92 | $manager = $app['chatkit']; 93 | 94 | return $manager->connection(); 95 | }); 96 | 97 | $this->app->alias('chatkit.connection', Chatkit::class); 98 | } 99 | 100 | /** 101 | * Get the services provided by the provider. 102 | * 103 | * @return string[] 104 | */ 105 | public function provides(): array 106 | { 107 | return [ 108 | 'chatkit', 109 | 'chatkit.factory', 110 | 'chatkit.connection', 111 | ]; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/Facades/Chatkit.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class Chatkit extends Facade 15 | { 16 | /** 17 | * Get the key of the component in the DI container the Facade proxies to 18 | * 19 | * @return string 20 | */ 21 | protected static function getFacadeAccessor(): string 22 | { 23 | return 'chatkit'; 24 | } 25 | } 26 | --------------------------------------------------------------------------------