Updated docs

This commit is contained in:
Alex Renoki 2020-09-11 09:25:13 +03:00
parent 18dab98d87
commit cc5e74e7e2
14 changed files with 48 additions and 92 deletions

View File

@ -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. > 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: This is what it looks like:
@ -34,11 +34,11 @@ interface AppManager
The following is an example AppManager that utilizes an Eloquent model: The following is an example AppManager that utilizes an Eloquent model:
```php ```php
namespace App\Appmanagers; namespace App\Managers;
use App\Application; use App\Application;
use BeyondCode\LaravelWebSockets\Apps\App; use BeyondCode\LaravelWebSockets\Apps\App;
use BeyondCode\LaravelWebSockets\Apps\AppManager; use BeyondCode\LaravelWebSockets\Contracts\AppManager;
class MyCustomAppManager implements AppManager class MyCustomAppManager implements AppManager
{ {
@ -116,7 +116,5 @@ Once you have implemented your own AppManager, you need to set it in the `websoc
'app' => \App\Managers\MyCustomAppManager::class, 'app' => \App\Managers\MyCustomAppManager::class,
...
], ],
``` ```

View File

@ -15,13 +15,13 @@ Once implemented, you will have a class that looks something like this:
```php ```php
namespace App; namespace App;
use Exception;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface; use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface; use Ratchet\WebSocket\MessageComponentInterface;
class MyCustomWebSocketHandler implements MessageComponentInterface class MyCustomWebSocketHandler implements MessageComponentInterface
{ {
public function onOpen(ConnectionInterface $connection) public function onOpen(ConnectionInterface $connection)
{ {
// TODO: Implement onOpen() method. // TODO: Implement onOpen() method.
@ -32,7 +32,7 @@ class MyCustomWebSocketHandler implements MessageComponentInterface
// TODO: Implement onClose() method. // TODO: Implement onClose() method.
} }
public function onError(ConnectionInterface $connection, \Exception $e) public function onError(ConnectionInterface $connection, Exception $e)
{ {
// TODO: Implement onError() method. // 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. 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. This could, for example, be done inside your `routes/web.php` file.
```php ```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. Once you've added the custom WebSocket route, be sure to restart our WebSocket server for the changes to take place.

View File

@ -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;
}
}
```

View File

@ -36,7 +36,7 @@ class WebSocketHandler extends BaseWebSocketHandler
// Run code on close. // Run code on close.
// $connection->app contains the app details // $connection->app contains the app details
// $this->channelManager is accessible // $this->channelManager is accessible
}**** }
} }
``` ```

View File

@ -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. 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 ```bash
composer require pusher/pusher-php-server "~4.0" 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 ## 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. 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/6.0/broadcasting#receiving-broadcasts). 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. 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 ```js
import Echo from "laravel-echo" import Echo from 'laravel-echo';
window.Pusher = require('pusher-js'); 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).

View File

@ -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. 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 ```bash
php artisan websockets:restart php artisan websockets:restart

View File

@ -10,6 +10,7 @@ Since most of the web's traffic is going through HTTPS, it's also crucial to sec
## Configuration ## Configuration
The SSL configuration takes place in your `config/websockets.php` file. The SSL configuration takes place in your `config/websockets.php` file.
The default configuration has a SSL section that looks like this: The default configuration has a SSL section that looks like this:
```php ```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. 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). 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. 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.

View File

@ -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. 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 ```bash
// 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class, php artisan websockets:serve --disable-statistics
'statistics_logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class, // use the `NullStatisticsLogger` instead
``` ```
## 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 ## Event Creator
The dashboard also comes with an easy-to-use event creator, that lets you manually send events to your channels. The dashboard also comes with an easy-to-use event creator, that lets you manually send events to your channels.

View File

@ -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) ![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. 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.

View File

@ -21,7 +21,7 @@ php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsSe
# Statistics # 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: You can publish the migration file using:

View File

@ -4,9 +4,10 @@ order: 1
--- ---
# Laravel WebSockets 🛰 # Laravel WebSockets 🛰
WebSockets for Laravel. Done right. 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: Once installed, you can start it with one simple command:

View File

@ -15,12 +15,12 @@ For example, Redis does a great job by encapsulating the both the way of notifyi
## Configure the replication ## 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 ```php
'replication' => [ 'replication' => [
'driver' => 'redis', 'mode' => 'redis',
... ...

View File

@ -1,16 +1,20 @@
--- ---
title: Redis title: Redis Mode
order: 2 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 ```php
'replication' => [ 'replication' => [
'driver' => 'redis', 'mode' => 'redis',
... ...
@ -22,7 +26,7 @@ You can set the connection name to the Redis database under `redis`:
```php ```php
'replication' => [ 'replication' => [
... 'modes' =>
'redis' => [ 'redis' => [
@ -31,6 +35,8 @@ You can set the connection name to the Redis database under `redis`:
], ],
], ],
],
``` ```
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.