├── LICENSE ├── README.md ├── composer.json ├── config └── realtime.php └── src ├── ConnectionManager.php ├── Connections ├── NullConnection.php ├── PubNubConnection.php └── PusherConnection.php ├── Connectors ├── PubNubConnector.php └── PusherConnector.php ├── Contracts ├── Connection.php └── Factory.php ├── Facades └── Realtime.php └── RealtimeServiceProvider.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 duxet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-realtime 2 | [![Gitter chat](https://badges.gitter.im/duxet/laravel-realtime.png)](https://gitter.im/duxet/laravel-realtime) 3 | [![Build Status](https://travis-ci.org/duxet/laravel-realtime.svg?branch=master)](https://travis-ci.org/duxet/laravel-realtime) 4 | [![Test Coverage](https://codeclimate.com/github/duxet/laravel-realtime/badges/coverage.svg)](https://codeclimate.com/github/duxet/laravel-realtime) 5 | [![Code Climate](https://codeclimate.com/github/duxet/laravel-realtime/badges/gpa.svg)](https://codeclimate.com/github/duxet/laravel-realtime) 6 | [![Packagist](https://img.shields.io/packagist/dt/duxet/laravel-realtime.svg)](https://packagist.org/packages/duxet/laravel-realtime) 7 | 8 | Laravel package for realtime communication using publish/subscribe pattern. 9 | 10 | # What is that? 11 | Ajax is not cool anymore. Now we have websockets, which gives us realtime communication with minimal delay. But how to use it with Laravel? This package gives answer to this question! 12 | 13 | # Supported services 14 | * PubNub 15 | * Pusher (only publish method) 16 | 17 | # How to use it? 18 | ```php 19 | Realtime::publish('my_channel', 'Hello world!'); 20 | ``` 21 | 22 | ```php 23 | Realtime::subscribe('my_channel', function($message) { 24 | ... 25 | ); 26 | ``` 27 | 28 | # Installation 29 | Require this package by using following command: 30 | 31 | ``` 32 | composer require duxet/laravel-realtime 33 | ``` 34 | 35 | After updating composer, add the ServiceProvider to the providers array in `config/app.php` 36 | 37 | ```php 38 | 'duxet\Realtime\RealtimeServiceProvider', 39 | ``` 40 | 41 | And if you want, you can add alias to Facade in your 'config/app.php' 42 | ```php 43 | 'Realtime' => 'duxet\Realtime\Facades\Realtime', 44 | ``` 45 | 46 | # License 47 | Package is licensed under MIT License. 48 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "duxet/laravel-realtime", 3 | "description": "Laravel realtime communication using pub/sub pattern", 4 | "keywords": ["laravel", "realtime", "pubsub"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "duxet", 9 | "email": "duxetlg@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0", 14 | "illuminate/support": "5.0.*", 15 | "graham-campbell/manager": "~2.0" 16 | }, 17 | "require-dev": { 18 | "graham-campbell/testbench": "~2.0", 19 | "phpunit/phpunit": "~4.4", 20 | "mockery/mockery": "~0.9", 21 | "codeclimate/php-test-reporter": "0.1.*" 22 | }, 23 | "suggest": { 24 | "pubnub/pubnub": "PubNub support.", 25 | "pusher/pusher-php-server": "Pusher support." 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "duxet\\Realtime\\": "src/" 30 | } 31 | }, 32 | "autoload-dev": { 33 | "psr-4": { 34 | "duxet\\Tests\\Realtime\\": "tests/" 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /config/realtime.php: -------------------------------------------------------------------------------- 1 | 'null', 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Connections 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here are each of the connections setup for your application. Example 24 | | configuration has been included, but you may add as many connections as 25 | | you would like. 26 | | 27 | */ 28 | 29 | 'connections' => [ 30 | 31 | 'pubnub' => [ 32 | 'driver' => 'pubnub', 33 | 'publish_key' => env('PUBNUB_PUBLISH_KEY'), 34 | 'subscribe_key' => env('PUBNUB_SUBSCRIBE_KEY'), 35 | 'secret_key' => env('PUBNUB_SECRET_KEY'), 36 | 'ssl' => env('PUBNUB_SSL', true), 37 | ], 38 | 39 | 'pusher' => [ 40 | 'driver' => 'pusher', 41 | 'app_id' => env('PUSHER_APP_ID'), 42 | 'key' => env('PUSHER_KEY'), 43 | 'secret' => env('PUSHER_SECRET'), 44 | 'ssl' => env('PUSHER_SSL', true), 45 | ], 46 | 47 | 'null' => [ 48 | 'driver' => 'null', 49 | ] 50 | 51 | ], 52 | 53 | ]; 54 | -------------------------------------------------------------------------------- /src/ConnectionManager.php: -------------------------------------------------------------------------------- 1 | pubnub = $pubnub; 21 | } 22 | 23 | /** 24 | * Publish new message on given channel. 25 | * 26 | * @param string $channel 27 | * @param mixed $message 28 | * @return bool 29 | */ 30 | public function publish($channel, $message) 31 | { 32 | $result = $this->pubnub->publish($channel, $message); 33 | 34 | if (isset($result['error'])) 35 | { 36 | throw new Exception('PubNub error: '. $result['message']); 37 | } 38 | 39 | return $result[1] === 'Sent'; 40 | } 41 | 42 | /** 43 | * Subscribe to given channel. 44 | * 45 | * @param string $channel 46 | * @param Closure $callback 47 | * @return void 48 | */ 49 | public function subscribe($channel, Closure $callback) 50 | { 51 | $this->pubnub->subscribe($channel, $callback); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/Connections/PusherConnection.php: -------------------------------------------------------------------------------- 1 | pusher = $pusher; 20 | } 21 | 22 | /** 23 | * Publish new message on given channel. 24 | * 25 | * @param string $channel 26 | * @param mixed $message 27 | * @return bool 28 | */ 29 | public function publish($channel, $message) 30 | { 31 | $this->pusher->trigger($channel, 'message', $message); 32 | } 33 | 34 | /** 35 | * Subscribe to given channel. 36 | * 37 | * @param string $channel 38 | * @param Closure $callback 39 | * @throws Exception 40 | */ 41 | public function subscribe($channel, Closure $callback) 42 | { 43 | throw new Exception('Subscribe method is not implemented for Pusher yet.'); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/Connectors/PubNubConnector.php: -------------------------------------------------------------------------------- 1 | $config['ssl'] 21 | ] 22 | ); 23 | 24 | return new PusherConnection($pusher); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Contracts/Connection.php: -------------------------------------------------------------------------------- 1 | setupConfig(); 25 | } 26 | 27 | /** 28 | * Setup configuration files. 29 | * 30 | * @return void 31 | */ 32 | protected function setupConfig() 33 | { 34 | $sourcePath = realpath(__DIR__.'/../config/realtime.php'); 35 | $targetPath = config_path('realtime.php'); 36 | 37 | $this->publishes([ $sourcePath => $targetPath ]); 38 | $this->mergeConfigFrom($sourcePath, 'pusher'); 39 | } 40 | 41 | /** 42 | * Register the service provider. 43 | * 44 | * @return void 45 | */ 46 | public function register() 47 | { 48 | $this->app->singleton('realtime', function($app) 49 | { 50 | $manager = new ConnectionManager($app['config']); 51 | $this->registerConnectors($manager); 52 | 53 | return $manager; 54 | }); 55 | 56 | $this->app->singleton('realtime.connection', function($app) 57 | { 58 | return $app['realtime']->connection(); 59 | }); 60 | } 61 | 62 | /** 63 | * Register available connectors. 64 | * 65 | * @param ConnectionManager $manager 66 | * 67 | * @return void 68 | */ 69 | protected function registerConnectors(ConnectionManager $manager) 70 | { 71 | $manager->extend('null', function() { 72 | return new NullConnection; 73 | }); 74 | 75 | $manager->extend('pubnub', function($config) { 76 | return with(new PubNubConnector())->connect($config); 77 | }); 78 | 79 | $manager->extend('pusher', function($config) { 80 | return with(new PusherConnector())->connect($config); 81 | }); 82 | } 83 | 84 | /** 85 | * Get the services provided by the provider. 86 | * 87 | * @return array 88 | */ 89 | public function provides() 90 | { 91 | return ['realtime', 'realtime.connection']; 92 | } 93 | 94 | } 95 | --------------------------------------------------------------------------------