diff --git a/docs/_index.md b/docs/_index.md index 183f7e6..7c504e5 100644 --- a/docs/_index.md +++ b/docs/_index.md @@ -1,4 +1,4 @@ --- packageName: Laravel Websockets githubUrl: https://github.com/beyondcode/laravel-websockets ---- \ No newline at end of file +--- diff --git a/docs/advanced-usage/app-providers.md b/docs/advanced-usage/app-providers.md index aca721d..77f4502 100644 --- a/docs/advanced-usage/app-providers.md +++ b/docs/advanced-usage/app-providers.md @@ -11,7 +11,7 @@ Depending on your setup, you might have your app configuration stored elsewhere > Make sure that you do **not** perform any IO blocking tasks in your `AppManager`, as they will interfere with the asynchronous WebSocket execution. -In order to create your custom `AppManager`, create a class that implements the `BeyondCode\LaravelWebSockets\Apps\AppManager` interface. +In order to create your custom `AppManager`, create a class that implements the `BeyondCode\LaravelWebSockets\Contracts\AppManager` interface. This is what it looks like: @@ -34,11 +34,11 @@ interface AppManager The following is an example AppManager that utilizes an Eloquent model: ```php -namespace App\Appmanagers; +namespace App\Managers; use App\Application; use BeyondCode\LaravelWebSockets\Apps\App; -use BeyondCode\LaravelWebSockets\Apps\AppManager; +use BeyondCode\LaravelWebSockets\Contracts\AppManager; class MyCustomAppManager implements AppManager { @@ -51,22 +51,22 @@ class MyCustomAppManager implements AppManager ->toArray(); } - public function findById($appId) : ? App + public function findById($appId) : ?App { return $this->normalize(Application::findById($appId)->toArray()); } - public function findByKey($appKey) : ? App + public function findByKey($appKey) : ?App { return $this->normalize(Application::findByKey($appKey)->toArray()); } - public function findBySecret($appSecret) : ? App + public function findBySecret($appSecret) : ?App { return $this->normalize(Application::findBySecret($appSecret)->toArray()); } - protected function normalize(?array $appAttributes) : ? App + protected function normalize(?array $appAttributes) : ?App { if (! $appAttributes) { return null; @@ -116,7 +116,5 @@ Once you have implemented your own AppManager, you need to set it in the `websoc 'app' => \App\Managers\MyCustomAppManager::class, - ... - ], ``` diff --git a/docs/advanced-usage/custom-websocket-handlers.md b/docs/advanced-usage/custom-websocket-handlers.md index b7653d6..71ebe60 100644 --- a/docs/advanced-usage/custom-websocket-handlers.md +++ b/docs/advanced-usage/custom-websocket-handlers.md @@ -15,13 +15,13 @@ Once implemented, you will have a class that looks something like this: ```php namespace App; +use Exception; use Ratchet\ConnectionInterface; use Ratchet\RFC6455\Messaging\MessageInterface; use Ratchet\WebSocket\MessageComponentInterface; class MyCustomWebSocketHandler implements MessageComponentInterface { - public function onOpen(ConnectionInterface $connection) { // TODO: Implement onOpen() method. @@ -32,7 +32,7 @@ class MyCustomWebSocketHandler implements MessageComponentInterface // TODO: Implement onClose() method. } - public function onError(ConnectionInterface $connection, \Exception $e) + public function onError(ConnectionInterface $connection, Exception $e) { // TODO: Implement onError() method. } @@ -48,12 +48,12 @@ In the class itself you have full control over all the lifecycle events of your The only part missing is, that you will need to tell our WebSocket server to load this handler at a specific route endpoint. This can be achieved using the `WebSocketsRouter` facade. -This class takes care of registering the routes with the actual webSocket server. You can use the `webSocket` method to define a custom WebSocket endpoint. The method needs two arguments: the path where the WebSocket handled should be available and the fully qualified classname of the WebSocket handler class. +This class takes care of registering the routes with the actual webSocket server. You can use the `get` method to define a custom WebSocket endpoint. The method needs two arguments: the path where the WebSocket handled should be available and the fully qualified classname of the WebSocket handler class. This could, for example, be done inside your `routes/web.php` file. ```php -WebSocketsRouter::webSocket('/my-websocket', \App\MyCustomWebSocketHandler::class); +WebSocketsRouter::get('/my-websocket', \App\MyCustomWebSocketHandler::class); ``` Once you've added the custom WebSocket route, be sure to restart our WebSocket server for the changes to take place. diff --git a/docs/advanced-usage/events.md b/docs/advanced-usage/events.md deleted file mode 100644 index 7e8ba3a..0000000 --- a/docs/advanced-usage/events.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -title: Triggered Events -order: 4 ---- - -# Triggered Events - -When an user subscribes or unsubscribes from a channel, a Laravel event gets triggered. - -- Connection subscribed channel: `\BeyondCode\LaravelWebSockets\Events\Subscribed` -- Connection left channel: `\BeyondCode\LaravelWebSockets\Events\Unsubscribed` - -You can listen to them by [registering them in the EventServiceProvider](https://laravel.com/docs/7.x/events#registering-events-and-listeners) and attaching Listeners to them. - -```php -/** - * The event listener mappings for the application. - * - * @var array - */ -protected $listen = [ - 'BeyondCode\LaravelWebSockets\Events\Subscribed' => [ - 'App\Listeners\SomeListener', - ], -]; -``` - -You will be provided the connection and the channel name through the event: - -```php -class SomeListener -{ - public function handle($event) - { - // You can access: - // $event->connection - // $event->channelName - - // You can also retrieve the app: - $app = $event->connection->app; - - // Or the socket ID: - $socketId = $event->connection->socketId; - } -} -``` diff --git a/docs/advanced-usage/webhooks.md b/docs/advanced-usage/webhooks.md index ca4799e..2df8e92 100644 --- a/docs/advanced-usage/webhooks.md +++ b/docs/advanced-usage/webhooks.md @@ -36,7 +36,7 @@ class WebSocketHandler extends BaseWebSocketHandler // Run code on close. // $connection->app contains the app details // $this->channelManager is accessible - }**** + } } ``` diff --git a/docs/basic-usage/pusher.md b/docs/basic-usage/pusher.md index 219e2c1..6d72a2d 100644 --- a/docs/basic-usage/pusher.md +++ b/docs/basic-usage/pusher.md @@ -13,7 +13,7 @@ To make it clear, the package does not restrict connections numbers or depend on To make use of the Laravel WebSockets package in combination with Pusher, you first need to install the official Pusher PHP SDK. -If you are not yet familiar with the concept of Broadcasting in Laravel, please take a look at the [Laravel documentation](https://laravel.com/docs/6.0/broadcasting). +If you are not yet familiar with the concept of Broadcasting in Laravel, please take a look at the [Laravel documentation](https://laravel.com/docs/8.0/broadcasting). ```bash composer require pusher/pusher-php-server "~4.0" @@ -99,8 +99,8 @@ To enable or disable the statistics for one of your apps, you can modify the `en ## Usage with Laravel Echo -The Laravel WebSockets package integrates nicely into [Laravel Echo](https://laravel.com/docs/6.0/broadcasting#receiving-broadcasts) to integrate into your frontend application and receive broadcasted events. -If you are new to Laravel Echo, be sure to take a look at the [official documentation](https://laravel.com/docs/6.0/broadcasting#receiving-broadcasts). +The Laravel WebSockets package integrates nicely into [Laravel Echo](https://laravel.com/docs/8.0/broadcasting#receiving-broadcasts) to integrate into your frontend application and receive broadcasted events. +If you are new to Laravel Echo, be sure to take a look at the [official documentation](https://laravel.com/docs/8.0/broadcasting#receiving-broadcasts). To make Laravel Echo work with Laravel WebSockets, you need to make some minor configuration changes when working with Laravel Echo. Add the `wsHost` and `wsPort` parameters and point them to your Laravel WebSocket server host and port. @@ -111,7 +111,7 @@ When using Laravel WebSockets in combination with a custom SSL certificate, be s ::: ```js -import Echo from "laravel-echo" +import Echo from 'laravel-echo'; window.Pusher = require('pusher-js'); @@ -126,4 +126,4 @@ window.Echo = new Echo({ }); ``` -Now you can use all Laravel Echo features in combination with Laravel WebSockets, such as [Presence Channels](https://laravel.com/docs/7.x/broadcasting#presence-channels), [Notifications](https://laravel.com/docs/7.x/broadcasting#notifications) and [Client Events](https://laravel.com/docs/7.x/broadcasting#client-events). +Now you can use all Laravel Echo features in combination with Laravel WebSockets, such as [Presence Channels](https://laravel.com/docs/8.x/broadcasting#presence-channels), [Notifications](https://laravel.com/docs/8.x/broadcasting#notifications) and [Client Events](https://laravel.com/docs/8.x/broadcasting#client-events). diff --git a/docs/basic-usage/restarting.md b/docs/basic-usage/restarting.md index f4b19fd..56c5539 100644 --- a/docs/basic-usage/restarting.md +++ b/docs/basic-usage/restarting.md @@ -7,7 +7,7 @@ order: 4 If you use Supervisor to keep your server alive, you might want to restart it just like `queue:restart` does. -To do so, consider using the `websockets:restart`. In a maximum of 10 seconds, the server will be restarted automatically. +To do so, consider using the `websockets:restart`. In a maximum of 10 seconds since issuing the command, the server will be restarted. ```bash php artisan websockets:restart diff --git a/docs/basic-usage/ssl.md b/docs/basic-usage/ssl.md index 5320840..3e09369 100644 --- a/docs/basic-usage/ssl.md +++ b/docs/basic-usage/ssl.md @@ -10,6 +10,7 @@ Since most of the web's traffic is going through HTTPS, it's also crucial to sec ## Configuration The SSL configuration takes place in your `config/websockets.php` file. + The default configuration has a SSL section that looks like this: ```php @@ -31,6 +32,7 @@ The default configuration has a SSL section that looks like this: ``` But this is only a subset of all the available configuration options. + This packages makes use of the official PHP [SSL context options](http://php.net/manual/en/context.ssl.php). So if you find yourself in the need of adding additional configuration settings, take a look at the PHP documentation and simply add the configuration parameters that you need. diff --git a/docs/debugging/dashboard.md b/docs/debugging/dashboard.md index a108a8c..bba0551 100644 --- a/docs/debugging/dashboard.md +++ b/docs/debugging/dashboard.md @@ -71,21 +71,12 @@ protected function schedule(Schedule $schedule) Each app contains an `enable_statistics` that defines wether that app generates statistics or not. The statistics are being stored for the `interval_in_seconds` seconds and then they are inserted in the database. -However, to disable it entirely and void any incoming statistic, you can change the statistics logger to `NullStatisticsLogger` under your current replication driver. +However, to disable it entirely and void any incoming statistic, you can call `--disable-statistics` when running the server command: -```php -// 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class, -'statistics_logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class, // use the `NullStatisticsLogger` instead +```bash +php artisan websockets:serve --disable-statistics ``` -## Custom Statistics Drivers - -By default, the package comes with a few drivers like the Database driver which stores the data into the database. - -You should add your custom drivers under the `statistics` key in `websockets.php` and create a driver class that implements the `\BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver` interface. - -Take a quick look at the `\BeyondCode\LaravelWebSockets\Statistics\Drivers\DatabaseDriver` driver to see how to perform your integration. - ## Event Creator The dashboard also comes with an easy-to-use event creator, that lets you manually send events to your channels. diff --git a/docs/faq/scaling.md b/docs/faq/scaling.md index aa19abd..b5033f0 100644 --- a/docs/faq/scaling.md +++ b/docs/faq/scaling.md @@ -16,3 +16,7 @@ Here is another benchmark that was run on a 2GB Digital Ocean droplet with 2 CPU ![Benchmark](/img/simultaneous_users_2gb.png) Make sure to take a look at the [Deployment Tips](/docs/laravel-websockets/faq/deploying) to find out how to improve your specific setup. + +# Horizontal Scaling + +When deploying to multi-node environments, you will notice that the server won't behave correctly. Check [Horizontal Scaling](../horizontal-scaling/getting-started.md) section. diff --git a/docs/getting-started/installation.md b/docs/getting-started/installation.md index 824489b..5d24d7d 100644 --- a/docs/getting-started/installation.md +++ b/docs/getting-started/installation.md @@ -21,7 +21,7 @@ php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsSe # Statistics -This package comes with a migration to store statistic information while running your WebSocket server. For more info, check the [Debug Dashboard](../debugging/dashboard.md) section. +This package comes with migrations to store statistic information while running your WebSocket server. For more info, check the [Debug Dashboard](../debugging/dashboard.md) section. You can publish the migration file using: diff --git a/docs/getting-started/introduction.md b/docs/getting-started/introduction.md index e061c8a..0e5050a 100644 --- a/docs/getting-started/introduction.md +++ b/docs/getting-started/introduction.md @@ -4,9 +4,10 @@ order: 1 --- # Laravel WebSockets 🛰 + WebSockets for Laravel. Done right. -Laravel WebSockets is a package for Laravel 5.7 and up that will get your application started with WebSockets in no-time! It has a drop-in Pusher API replacement, has a debug dashboard, realtime statistics and even allows you to create custom WebSocket controllers. +Laravel WebSockets is a package for Laravel that will get your application started with WebSockets in no-time! It has a drop-in Pusher API replacement, has a debug dashboard, realtime statistics and even allows you to create custom WebSocket controllers. Once installed, you can start it with one simple command: @@ -18,4 +19,4 @@ php artisan websockets:serve If you want to know how all of it works under the hood, we wrote an in-depth [blogpost](https://murze.be/introducing-laravel-websockets-an-easy-to-use-websocket-server-implemented-in-php) about it. -To help you get started, you can also take a look at the [demo repository](https://github.com/beyondcode/laravel-websockets-demo), that implements a basic Chat built with this package. \ No newline at end of file +To help you get started, you can also take a look at the [demo repository](https://github.com/beyondcode/laravel-websockets-demo), that implements a basic Chat built with this package. diff --git a/docs/horizontal-scaling/getting-started.md b/docs/horizontal-scaling/getting-started.md index fffd7fa..1bb3ab4 100644 --- a/docs/horizontal-scaling/getting-started.md +++ b/docs/horizontal-scaling/getting-started.md @@ -15,12 +15,12 @@ For example, Redis does a great job by encapsulating the both the way of notifyi ## Configure the replication -To enable the replication, simply change the `replication.driver` name in the `websockets.php` file: +To enable the replication, simply change the `replication.mode` name in the `websockets.php` file: ```php 'replication' => [ - 'driver' => 'redis', + 'mode' => 'redis', ... diff --git a/docs/horizontal-scaling/redis.md b/docs/horizontal-scaling/redis.md index 55020fe..4f63835 100644 --- a/docs/horizontal-scaling/redis.md +++ b/docs/horizontal-scaling/redis.md @@ -1,16 +1,20 @@ --- -title: Redis +title: Redis Mode order: 2 --- -## Configure the Redis driver +# Redis Mode -To enable the replication, simply change the `replication.driver` name in the `websockets.php` file to `redis`: +Redis has the powerful ability to act both as a key-value store and as a PubSub service. This way, the connected servers will communicate between them whenever a message hits the server, so you can scale out to any amount of servers while preserving the WebSockets functionalities. + +## Configure Redis mode + +To enable the replication, simply change the `replication.mode` name in the `websockets.php` file to `redis`: ```php 'replication' => [ - 'driver' => 'redis', + 'mode' => 'redis', ... @@ -22,15 +26,17 @@ You can set the connection name to the Redis database under `redis`: ```php 'replication' => [ - ... + 'modes' => - 'redis' => [ + 'redis' => [ - 'connection' => 'default', + 'connection' => 'default', + + ], ], ], ``` -The connections can be found in your `config/database.php` file, under the `redis` key. It defaults to connection `default`. +The connections can be found in your `config/database.php` file, under the `redis` key.