laravel-websockets/docs/advanced-usage/app-providers.md

121 lines
3.6 KiB
Markdown
Raw Normal View History

2020-08-18 12:29:49 +00:00
---
title: Custom App Managers
order: 1
---
# Custom App Managers
2020-06-02 08:56:13 +00:00
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.
2020-08-18 12:29:49 +00:00
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.
2020-06-02 08:56:13 +00:00
2020-08-18 12:29:49 +00:00
> Make sure that you do **not** perform any IO blocking tasks in your `AppManager`, as they will interfere with the asynchronous WebSocket execution.
2020-06-02 08:56:13 +00:00
2025-01-16 07:54:02 +00:00
In order to create your custom `AppManager`, create a class that implements the `BlaxSoftware\LaravelWebSockets\Contracts\AppManager` interface.
2020-06-02 08:56:13 +00:00
This is what it looks like:
```php
2020-08-18 12:29:49 +00:00
interface AppManager
2020-06-02 08:56:13 +00:00
{
2025-01-16 07:54:02 +00:00
/** @return array[BlaxSoftware\LaravelWebSockets\Apps\App] */
2020-06-02 08:56:13 +00:00
public function all(): array;
2025-01-16 07:54:02 +00:00
/** @return BlaxSoftware\LaravelWebSockets\Apps\App */
2020-06-02 08:56:13 +00:00
public function findById($appId): ?App;
2025-01-16 07:54:02 +00:00
/** @return BlaxSoftware\LaravelWebSockets\Apps\App */
2020-08-18 17:21:22 +00:00
public function findByKey($appKey): ?App;
2020-06-02 08:56:13 +00:00
2025-01-16 07:54:02 +00:00
/** @return BlaxSoftware\LaravelWebSockets\Apps\App */
2020-08-18 17:21:22 +00:00
public function findBySecret($appSecret): ?App;
2020-06-02 08:56:13 +00:00
}
```
2020-08-18 12:29:49 +00:00
The following is an example AppManager that utilizes an Eloquent model:
2020-06-02 08:56:13 +00:00
```php
2020-09-11 06:25:13 +00:00
namespace App\Managers;
2020-06-02 08:56:13 +00:00
use App\Application;
2025-01-16 07:54:02 +00:00
use BlaxSoftware\LaravelWebSockets\Apps\App;
use BlaxSoftware\LaravelWebSockets\Contracts\AppManager;
2020-06-02 08:56:13 +00:00
2020-08-18 12:29:49 +00:00
class MyCustomAppManager implements AppManager
2020-06-02 08:56:13 +00:00
{
public function all() : array
{
return Application::all()
->map(function($app) {
2020-08-18 12:29:49 +00:00
return $this->normalize($app->toArray());
2020-06-02 08:56:13 +00:00
})
->toArray();
}
2020-09-11 06:25:13 +00:00
public function findById($appId) : ?App
2020-06-02 08:56:13 +00:00
{
2020-08-18 12:29:49 +00:00
return $this->normalize(Application::findById($appId)->toArray());
2020-06-02 08:56:13 +00:00
}
2020-09-11 06:25:13 +00:00
public function findByKey($appKey) : ?App
2020-06-02 08:56:13 +00:00
{
2020-08-18 12:29:49 +00:00
return $this->normalize(Application::findByKey($appKey)->toArray());
2020-06-02 08:56:13 +00:00
}
2020-09-11 06:25:13 +00:00
public function findBySecret($appSecret) : ?App
2020-06-02 08:56:13 +00:00
{
2020-08-18 12:29:49 +00:00
return $this->normalize(Application::findBySecret($appSecret)->toArray());
2020-06-02 08:56:13 +00:00
}
2020-09-11 06:25:13 +00:00
protected function normalize(?array $appAttributes) : ?App
2020-06-02 08:56:13 +00:00
{
2020-08-18 12:29:49 +00:00
if (! $appAttributes) {
2020-06-02 08:56:13 +00:00
return null;
}
$app = new App(
$appAttributes['id'],
$appAttributes['key'],
$appAttributes['secret']
);
if (isset($appAttributes['name'])) {
$app->setName($appAttributes['name']);
}
if (isset($appAttributes['host'])) {
$app->setHost($appAttributes['host']);
}
$app
->enableClientMessages($appAttributes['enable_client_messages'])
->enableStatistics($appAttributes['enable_statistics']);
return $app;
}
}
```
2020-08-18 12:29:49 +00:00
Once you have implemented your own AppManager, you need to set it in the `websockets.php` configuration file:
```php
'managers' => [
/*
|--------------------------------------------------------------------------
| Application Manager
|--------------------------------------------------------------------------
|
| An Application manager determines how your websocket server allows
| the use of the TCP protocol based on, for example, a list of allowed
| applications.
| By default, it uses the defined array in the config file, but you can
| anytime implement the same interface as the class and add your own
| custom method to retrieve the apps.
|
*/
'app' => \App\Managers\MyCustomAppManager::class,
],
2020-06-02 08:56:13 +00:00
```