Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 560bc6e

Browse files
authored
Merge pull request #468 from beyondcode/feature/docs
[2.x] Docs
2 parents 5153568 + ffcc772 commit 560bc6e

18 files changed

+380
-235
lines changed

docs/advanced-usage/app-providers.md

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,74 @@
1-
# Custom App Providers
1+
---
2+
title: Custom App Managers
3+
order: 1
4+
---
5+
6+
# Custom App Managers
27

38
With the multi-tenancy support of Laravel WebSockets, the default way of storing and retrieving the apps is by using the `websockets.php` config file.
49

5-
Depending on your setup, you might have your app configuration stored elsewhere and having to keep the configuration in sync with your app storage can be tedious. To simplify this, you can create your own `AppProvider` class that will take care of retrieving the WebSocket credentials for a specific WebSocket application.
10+
Depending on your setup, you might have your app configuration stored elsewhere and having to keep the configuration in sync with your app storage can be tedious. To simplify this, you can create your own `AppManager` class that will take care of retrieving the WebSocket credentials for a specific WebSocket application.
611

7-
> Make sure that you do **not** perform any IO blocking tasks in your `AppProvider`, as they will interfere with the asynchronous WebSocket execution.
12+
> Make sure that you do **not** perform any IO blocking tasks in your `AppManager`, as they will interfere with the asynchronous WebSocket execution.
813
9-
In order to create your custom `AppProvider`, create a class that implements the `BeyondCode\LaravelWebSockets\AppProviders\AppProvider` interface.
14+
In order to create your custom `AppManager`, create a class that implements the `BeyondCode\LaravelWebSockets\Apps\AppManager` interface.
1015

1116
This is what it looks like:
1217

1318
```php
14-
interface AppProvider
19+
interface AppManager
1520
{
16-
/** @return array[BeyondCode\LaravelWebSockets\AppProviders\App] */
21+
/** @return array[BeyondCode\LaravelWebSockets\Apps\App] */
1722
public function all(): array;
1823

19-
/** @return BeyondCode\LaravelWebSockets\AppProviders\App */
24+
/** @return BeyondCode\LaravelWebSockets\Apps\App */
2025
public function findById($appId): ?App;
2126

22-
/** @return BeyondCode\LaravelWebSockets\AppProviders\App */
27+
/** @return BeyondCode\LaravelWebSockets\Apps\App */
2328
public function findByKey(string $appKey): ?App;
2429

25-
/** @return BeyondCode\LaravelWebSockets\AppProviders\App */
30+
/** @return BeyondCode\LaravelWebSockets\Apps\App */
2631
public function findBySecret(string $appSecret): ?App;
2732
}
2833
```
2934

30-
The following is an example AppProvider that utilizes an Eloquent model:
35+
The following is an example AppManager that utilizes an Eloquent model:
3136
```php
32-
namespace App\Providers;
37+
namespace App\Appmanagers;
3338

3439
use App\Application;
3540
use BeyondCode\LaravelWebSockets\Apps\App;
36-
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
41+
use BeyondCode\LaravelWebSockets\Apps\AppManager;
3742

38-
class MyCustomAppProvider implements AppProvider
43+
class MyCustomAppManager implements AppManager
3944
{
4045
public function all() : array
4146
{
4247
return Application::all()
4348
->map(function($app) {
44-
return $this->instanciate($app->toArray());
49+
return $this->normalize($app->toArray());
4550
})
4651
->toArray();
4752
}
4853

4954
public function findById($appId) : ? App
5055
{
51-
return $this->instanciate(Application::findById($appId)->toArray());
56+
return $this->normalize(Application::findById($appId)->toArray());
5257
}
5358

5459
public function findByKey(string $appKey) : ? App
5560
{
56-
return $this->instanciate(Application::findByKey($appKey)->toArray());
61+
return $this->normalize(Application::findByKey($appKey)->toArray());
5762
}
5863

5964
public function findBySecret(string $appSecret) : ? App
6065
{
61-
return $this->instanciate(Application::findBySecret($appSecret)->toArray());
66+
return $this->normalize(Application::findBySecret($appSecret)->toArray());
6267
}
6368

64-
protected function instanciate(?array $appAttributes) : ? App
69+
protected function normalize(?array $appAttributes) : ? App
6570
{
66-
if (!$appAttributes) {
71+
if (! $appAttributes) {
6772
return null;
6873
}
6974

@@ -90,15 +95,28 @@ class MyCustomAppProvider implements AppProvider
9095
}
9196
```
9297

93-
Once you have implemented your own AppProvider, you need to set it in the `websockets.php` configuration file:
94-
95-
```php
96-
/**
97-
* This class is responsible for finding the apps. The default provider
98-
* will use the apps defined in this config file.
99-
*
100-
* You can create a custom provider by implementing the
101-
* `AppProvider` interface.
102-
*/
103-
'app_provider' => MyCustomAppProvider::class,
98+
Once you have implemented your own AppManager, you need to set it in the `websockets.php` configuration file:
99+
100+
```php
101+
'managers' => [
102+
103+
/*
104+
|--------------------------------------------------------------------------
105+
| Application Manager
106+
|--------------------------------------------------------------------------
107+
|
108+
| An Application manager determines how your websocket server allows
109+
| the use of the TCP protocol based on, for example, a list of allowed
110+
| applications.
111+
| By default, it uses the defined array in the config file, but you can
112+
| anytime implement the same interface as the class and add your own
113+
| custom method to retrieve the apps.
114+
|
115+
*/
116+
117+
'app' => \App\Managers\MyCustomAppManager::class,
118+
119+
...
120+
121+
],
104122
```

docs/advanced-usage/custom-websocket-handlers.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
---
2+
title: Custom WebSocket Handlers
3+
order: 2
4+
---
5+
16
# Custom WebSocket Handlers
27

3-
While this package's main purpose is to make the usage of either the Pusher JavaScript client or Laravel Echo as easy as possible, you are not limited to the Pusher protocol at all.
8+
While this package's main purpose is to make the usage of either the Pusher JavaScript client or Laravel Echo as easy as possible, you are not limited to the Pusher protocol at all.
49
There might be situations where all you need is a simple, bare-bone, WebSocket server where you want to have full control over the incoming payload and what you want to do with it - without having "channels" in the way.
510

611
You can easily create your own custom WebSocketHandler class. All you need to do is implement Ratchets `Ratchet\WebSocket\MessageComponentInterface`.
@@ -21,7 +26,7 @@ class MyCustomWebSocketHandler implements MessageComponentInterface
2126
{
2227
// TODO: Implement onOpen() method.
2328
}
24-
29+
2530
public function onClose(ConnectionInterface $connection)
2631
{
2732
// TODO: Implement onClose() method.
@@ -51,4 +56,4 @@ This could, for example, be done inside your `routes/web.php` file.
5156
WebSocketsRouter::webSocket('/my-websocket', \App\MyCustomWebSocketHandler::class);
5257
```
5358

54-
Once you've added the custom WebSocket route, be sure to restart our WebSocket server for the changes to take place.
59+
Once you've added the custom WebSocket route, be sure to restart our WebSocket server for the changes to take place.

docs/advanced-usage/webhooks.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Webhooks
3+
order: 3
4+
---
5+
6+
# Webhooks
7+
8+
While you can create any custom websocket handlers, you might still want to intercept and run your own custom business logic on each websocket connection.
9+
10+
In Pusher, there are [Pusher Webhooks](https://pusher.com/docs/channels/server_api/webhooks) that do this job. However, since the implementation is a pure controller,
11+
you might want to extend it and update the config file to reflect the changes:
12+
13+
For example, running your own business logic on connection open and close:
14+
15+
```php
16+
namespace App\Controllers\WebSockets;
17+
18+
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler as BaseWebSocketHandler;
19+
use Ratchet\ConnectionInterface;
20+
21+
class WebSocketHandler extends BaseWebSocketHandler
22+
{
23+
public function onOpen(ConnectionInterface $connection)
24+
{
25+
parent::onOpen($connection);
26+
27+
// Run code on open
28+
// $connection->app contains the app details
29+
// $this->channelManager is accessible
30+
}
31+
32+
public function onClose(ConnectionInterface $connection)
33+
{
34+
parent::onClose($connection);
35+
36+
// Run code on close.
37+
// $connection->app contains the app details
38+
// $this->channelManager is accessible
39+
}****
40+
}
41+
```
42+
43+
Once you implemented it, replace the `handlers.websocket` class name in config:
44+
45+
```php
46+
'handlers' => [
47+
48+
'websocket' => App\Controllers\WebSockets\WebSocketHandler::class,
49+
50+
],
51+
```
52+
53+
A server restart is required afterwards.

docs/basic-usage/pusher.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ order: 1
77

88
The easiest way to get started with Laravel WebSockets is by using it as a [Pusher](https://pusher.com) replacement. The integrated WebSocket and HTTP Server has complete feature parity with the Pusher WebSocket and HTTP API. In addition to that, this package also ships with an easy to use debugging dashboard to see all incoming and outgoing WebSocket requests.
99

10+
To make it clear, the package does not restrict connections numbers or depend on the Pusher's service. It does comply with the Pusher protocol to make it easy to use the Pusher SDK with it.
11+
1012
## Requirements
1113

12-
To make use of the Laravel WebSockets package in combination with Pusher, you first need to install the official Pusher PHP SDK.
14+
To make use of the Laravel WebSockets package in combination with Pusher, you first need to install the official Pusher PHP SDK.
1315

1416
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).
1517

1618
```bash
17-
composer require pusher/pusher-php-server "~3.0"
19+
composer require pusher/pusher-php-server "~4.0"
1820
```
1921

2022
Next, you should make sure to use Pusher as your broadcasting driver. This can be achieved by setting the `BROADCAST_DRIVER` environment variable in your `.env` file:
@@ -40,7 +42,7 @@ To do this, you should add the `host` and `port` configuration key to your `conf
4042
'encrypted' => true,
4143
'host' => '127.0.0.1',
4244
'port' => 6001,
43-
'scheme' => 'http'
45+
'scheme' => 'http',
4446
],
4547
],
4648
```
@@ -68,6 +70,8 @@ You may add additional apps in your `config/websockets.php` file.
6870
'name' => env('APP_NAME'),
6971
'key' => env('PUSHER_APP_KEY'),
7072
'secret' => env('PUSHER_APP_SECRET'),
73+
'path' => env('PUSHER_APP_PATH'),
74+
'capacity' => null,
7175
'enable_client_messages' => false,
7276
'enable_statistics' => true,
7377
],
@@ -113,7 +117,8 @@ window.Echo = new Echo({
113117
wsPort: 6001,
114118
forceTLS: false,
115119
disableStats: true,
120+
enabledTransports: ['ws', 'wss'],
116121
});
117122
```
118123

119-
Now you can use all Laravel Echo features in combination with Laravel WebSockets, such as [Presence Channels](https://laravel.com/docs/6.0/broadcasting#presence-channels), [Notifications](https://laravel.com/docs/6.0/broadcasting#notifications) and [Client Events](https://laravel.com/docs/6.0/broadcasting#client-events).
124+
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).

docs/basic-usage/restarting.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Restarting Server
3+
order: 4
4+
---
5+
6+
# Restarting Server
7+
8+
If you use Supervisor to keep your server alive, you might want to restart it just like `queue:restart` does.
9+
10+
To do so, consider using the `websockets:restart`. In a maximum of 10 seconds, the server will be restarted automatically.
11+
12+
```bash
13+
php artisan websockets:restart
14+
```

docs/basic-usage/ssl.md

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,19 @@ The default configuration has a SSL section that looks like this:
1414

1515
```php
1616
'ssl' => [
17-
/*
18-
* Path to local certificate file on filesystem. It must be a PEM encoded file which
19-
* contains your certificate and private key. It can optionally contain the
20-
* certificate chain of issuers. The private key also may be contained
21-
* in a separate file specified by local_pk.
22-
*/
23-
'local_cert' => null,
24-
25-
/*
26-
* Path to local private key file on filesystem in case of separate files for
27-
* certificate (local_cert) and private key.
28-
*/
29-
'local_pk' => null,
30-
31-
/*
32-
* Passphrase with which your local_cert file was encoded.
33-
*/
34-
'passphrase' => null
17+
18+
'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),
19+
20+
'capath' => env('LARAVEL_WEBSOCKETS_SSL_CA', null),
21+
22+
'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),
23+
24+
'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
25+
26+
'verify_peer' => env('APP_ENV') === 'production',
27+
28+
'allow_self_signed' => env('APP_ENV') !== 'production',
29+
3530
],
3631
```
3732

@@ -62,7 +57,8 @@ window.Echo = new Echo({
6257
wsHost: window.location.hostname,
6358
wsPort: 6001,
6459
disableStats: true,
65-
forceTLS: true
60+
forceTLS: true,
61+
enabledTransports: ['ws', 'wss'],
6662
});
6763
```
6864

@@ -78,9 +74,10 @@ When broadcasting events from your Laravel application to the WebSocket server,
7874
'app_id' => env('PUSHER_APP_ID'),
7975
'options' => [
8076
'cluster' => env('PUSHER_APP_CLUSTER'),
77+
'encrypted' => true,
8178
'host' => '127.0.0.1',
8279
'port' => 6001,
83-
'scheme' => 'https'
80+
'scheme' => 'https',
8481
],
8582
],
8683
```
@@ -98,26 +95,19 @@ Make sure that you replace `YOUR-USERNAME` with your Mac username and `VALET-SIT
9895

9996
```php
10097
'ssl' => [
101-
/*
102-
* Path to local certificate file on filesystem. It must be a PEM encoded file which
103-
* contains your certificate and private key. It can optionally contain the
104-
* certificate chain of issuers. The private key also may be contained
105-
* in a separate file specified by local_pk.
106-
*/
98+
10799
'local_cert' => '/Users/YOUR-USERNAME/.config/valet/Certificates/VALET-SITE.TLD.crt',
108100

109-
/*
110-
* Path to local private key file on filesystem in case of separate files for
111-
* certificate (local_cert) and private key.
112-
*/
113-
'local_pk' => '/Users/YOUR-USERNAME/.config/valet/Certificates/VALET-SITE.TLD.key',
101+
'capath' => env('LARAVEL_WEBSOCKETS_SSL_CA', null),
102+
103+
'local_pk' => 'local_pk' => '/Users/YOUR-USERNAME/.config/valet/Certificates/VALET-SITE.TLD.key',
104+
105+
'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
106+
107+
'verify_peer' => env('APP_ENV') === 'production',
114108

115-
/*
116-
* Passphrase with which your local_cert file was encoded.
117-
*/
118-
'passphrase' => null,
109+
'allow_self_signed' => env('APP_ENV') !== 'production',
119110

120-
'verify_peer' => false,
121111
],
122112
```
123113

@@ -133,6 +123,7 @@ You also need to disable SSL verification.
133123
'app_id' => env('PUSHER_APP_ID'),
134124
'options' => [
135125
'cluster' => env('PUSHER_APP_CLUSTER'),
126+
'encrypted' => true,
136127
'host' => '127.0.0.1',
137128
'port' => 6001,
138129
'scheme' => 'https',

0 commit comments

Comments
 (0)