├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml └── src ├── FanoutBroadcastServiceProvider.php └── FanoutBroadcaster.php /.gitignore: -------------------------------------------------------------------------------- 1 | /bootstrap/compiled.php 2 | .env.*.php 3 | .env.php 4 | .env 5 | /vendor 6 | /.idea 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Fanout, Inc. 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | laravel-fanout 2 | ------------- 3 | Author: Konstantin Bokarius 4 | 5 | Fanout.io library for Laravel. 6 | 7 | Credit 8 | ------ 9 | 10 | This project is based on the following pull request by Christopher Thomas: https://github.com/cwt137/laravel-framework/commit/b64b57ec0dfac975db9fadecac9f7a3757b7af30 11 | 12 | Requirements 13 | ------------ 14 | 15 | * openssl 16 | * curl 17 | * pthreads (required for asynchronous publishing) 18 | * php >=7.0.0 19 | * laravel >= 5.5 20 | * fanout/fanout >=2.0.0 (retrieved automatically via Composer) 21 | 22 | Installation 23 | ------------ 24 | 25 | Using Composer: 26 | 27 | ```sh 28 | composer require fanout/laravel-fanout 29 | ``` 30 | 31 | Manual: ensure that php-fanout has been included and require the following files in laravel-fanout: 32 | 33 | ```PHP 34 | require 'laravel-fanout/src/fanoutbroadcaster.php'; 35 | require 'laravel-fanout/src/fanoutbroadcastserviceprovider.php'; 36 | ``` 37 | 38 | Asynchronous Publishing 39 | ----------------------- 40 | 41 | In order to make asynchronous publish calls pthreads must be installed. If pthreads is not installed then only synchronous publish calls can be made. To install pthreads recompile PHP with the following flag: '--enable-maintainer-zts' 42 | 43 | Also note that since a callback passed to the publish_async methods is going to be executed in a separate thread, that callback and the class it belongs to are subject to the rules and limitations imposed by the pthreads extension. 44 | 45 | See more information about pthreads here: http://php.net/manual/en/book.pthreads.php 46 | 47 | Usage 48 | ------------ 49 | 50 | In your config/broadcasting.php file set the default driver to 'fanout' and add the connection configuration like so: 51 | 52 | ```php 53 | 'default' => 'fanout', 54 | 55 | 'connections' => [ 56 | ... 57 | 'fanout' => [ 58 | 'driver' => 'fanout', 59 | 'realm_id' => '', 60 | 'realm_key' => '', 61 | 'ssl' => true, 62 | 'publish_async' => false 63 | ], 64 | ... 65 | ] 66 | ``` 67 | 68 | In your config/app.php file add the following provider to your service providers array: 69 | 70 | ```php 71 | 'providers' => [ 72 | ... 73 | LaravelFanout\FanoutBroadcastServiceProvider::class, 74 | ... 75 | ] 76 | ``` 77 | 78 | Add a custom broadcast event to your application like so: 79 | 80 | ```php 81 | namespace App\Events; 82 | 83 | use Illuminate\Queue\SerializesModels; 84 | use Illuminate\Contracts\Broadcasting\ShouldBroadcast; 85 | 86 | class PublishToFanoutEvent implements ShouldBroadcast 87 | { 88 | use SerializesModels; 89 | 90 | public $message; 91 | 92 | public function __construct($message) 93 | { 94 | $this->message = $message; 95 | } 96 | 97 | public function broadcastOn() 98 | { 99 | return ['']; 100 | } 101 | } 102 | ``` 103 | 104 | Now to publish to Fanout.io in your application simply fire the event: 105 | 106 | ```php 107 | event(new App\Events\PublishToFanoutEvent('Test publish!!')); 108 | ``` 109 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"fanout/laravel-fanout", 3 | "description":"Fanout.io library for Laravel.", 4 | "homepage":"https://github.com/fanout/laravel-fanout", 5 | "authors":[ 6 | { 7 | "name":"Konstantin Bokarius", 8 | "email":"kon@fanout.io", 9 | "role":"Developer" 10 | } 11 | ], 12 | "license":"MIT", 13 | "require":{ 14 | "php":">=7.0.0", 15 | "fanout/fanout":"^2.0.0" 16 | }, 17 | "require-dev":{ 18 | "phpunit/phpunit":"3.7.14" 19 | }, 20 | "autoload":{ 21 | "psr-4":{ 22 | "LaravelFanout\\":"src/" 23 | } 24 | }, 25 | "minimum-stability":"dev", 26 | "extra": { 27 | "laravel": { 28 | "providers": [ 29 | "LaravelFanout\\FanoutBroadcastServiceProvider" 30 | ] 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests/ 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/FanoutBroadcastServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->make( 19 | 'Illuminate\Broadcasting\BroadcastManager')->extend( 20 | 'fanout', function ($app, $config) { 21 | $ssl = true; 22 | if (array_key_exists('ssl', $config)) 23 | $ssl = $config['ssl']; 24 | $async = false; 25 | if (array_key_exists('publish_async', $config)) 26 | $async = $config['publish_async']; 27 | return new FanoutBroadcaster(new Fanout( 28 | $config['realm_id'], $config['realm_key'], $ssl), $async); 29 | }); 30 | } 31 | 32 | public function register() 33 | { 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /src/FanoutBroadcaster.php: -------------------------------------------------------------------------------- 1 | fanout = $fanout; 24 | $this->async = $async; 25 | } 26 | 27 | /** 28 | * Authenticate the incoming request for a given channel. 29 | * 30 | * @param \Illuminate\Http\Request $request 31 | * @return mixed 32 | */ 33 | public function auth($request) 34 | { 35 | 36 | } 37 | 38 | /** 39 | * Return the valid authentication response. 40 | * 41 | * @param \Illuminate\Http\Request $request 42 | * @param mixed $result 43 | * @return mixed 44 | */ 45 | public function validAuthenticationResponse($request, $result) 46 | { 47 | 48 | } 49 | 50 | /** 51 | * Broadcast the given event. 52 | * 53 | * @param array $channels 54 | * @param string $event 55 | * @param array $payload 56 | * @return void 57 | */ 58 | public function broadcast(array $channels, $event, array $payload = []) 59 | { 60 | $payload = ['event' => $event, 'data' => $payload]; 61 | foreach ($channels as $channel) { 62 | if ($this->async) 63 | $this->fanout->publish_async($channel, $payload); 64 | else 65 | $this->fanout->publish($channel, $payload); 66 | } 67 | } 68 | } 69 | --------------------------------------------------------------------------------