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

Commit 0b351d8

Browse files
committed
Add documentation
1 parent d7b9963 commit 0b351d8

20 files changed

+997
-1
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
/phpunit.xml.dist export-ignore
99
/.scrutinizer.yml export-ignore
1010
/tests export-ignore
11+
/docs export-ignore
1112
/.editorconfig export-ignore

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
build
22
composer.lock
3-
docs
43
vendor
54
coverage
65
.phpunit.result.cache

docs/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
packageName: Laravel Websockets
3+
githubUrl: https://github.com/beyondcode/laravel-websockets
4+
---

docs/advanced-usage/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Advanced Usage
3+
order: 4
4+
---
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Custom App Providers
2+
3+
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.
4+
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.
6+
7+
> Make sure that you do **not** perform any IO blocking tasks in your `AppProvider`, as they will interfere with the asynchronous WebSocket execution.
8+
9+
In order to create your custom `AppProvider`, create a class that implements the `BeyondCode\LaravelWebSockets\AppProviders\AppProvider` interface.
10+
11+
This is what it looks like:
12+
13+
```php
14+
interface AppProvider
15+
{
16+
/** @return array[BeyondCode\LaravelWebSockets\AppProviders\App] */
17+
public function all(): array;
18+
19+
/** @return BeyondCode\LaravelWebSockets\AppProviders\App */
20+
public function findById($appId): ?App;
21+
22+
/** @return BeyondCode\LaravelWebSockets\AppProviders\App */
23+
public function findByKey(string $appKey): ?App;
24+
25+
/** @return BeyondCode\LaravelWebSockets\AppProviders\App */
26+
public function findBySecret(string $appSecret): ?App;
27+
}
28+
```
29+
30+
The following is an example AppProvider that utilizes an Eloquent model:
31+
```php
32+
namespace App\Providers;
33+
34+
use App\Application;
35+
use BeyondCode\LaravelWebSockets\Apps\App;
36+
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
37+
38+
class MyCustomAppProvider implements AppProvider
39+
{
40+
public function all() : array
41+
{
42+
return Application::all()
43+
->map(function($app) {
44+
return $this->instanciate($app->toArray());
45+
})
46+
->toArray();
47+
}
48+
49+
public function findById($appId) : ? App
50+
{
51+
return $this->instanciate(Application::findById($appId)->toArray());
52+
}
53+
54+
public function findByKey(string $appKey) : ? App
55+
{
56+
return $this->instanciate(Application::findByKey($appKey)->toArray());
57+
}
58+
59+
public function findBySecret(string $appSecret) : ? App
60+
{
61+
return $this->instanciate(Application::findBySecret($appSecret)->toArray());
62+
}
63+
64+
protected function instanciate(?array $appAttributes) : ? App
65+
{
66+
if (!$appAttributes) {
67+
return null;
68+
}
69+
70+
$app = new App(
71+
$appAttributes['id'],
72+
$appAttributes['key'],
73+
$appAttributes['secret']
74+
);
75+
76+
if (isset($appAttributes['name'])) {
77+
$app->setName($appAttributes['name']);
78+
}
79+
80+
if (isset($appAttributes['host'])) {
81+
$app->setHost($appAttributes['host']);
82+
}
83+
84+
$app
85+
->enableClientMessages($appAttributes['enable_client_messages'])
86+
->enableStatistics($appAttributes['enable_statistics']);
87+
88+
return $app;
89+
}
90+
}
91+
```
92+
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,
104+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Custom WebSocket Handlers
2+
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.
4+
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.
5+
6+
You can easily create your own custom WebSocketHandler class. All you need to do is implement Ratchets `Ratchet\WebSocket\MessageComponentInterface`.
7+
8+
Once implemented, you will have a class that looks something like this:
9+
10+
```php
11+
namespace App;
12+
13+
use Ratchet\ConnectionInterface;
14+
use Ratchet\RFC6455\Messaging\MessageInterface;
15+
use Ratchet\WebSocket\MessageComponentInterface;
16+
17+
class MyCustomWebSocketHandler implements MessageComponentInterface
18+
{
19+
20+
public function onOpen(ConnectionInterface $connection)
21+
{
22+
// TODO: Implement onOpen() method.
23+
}
24+
25+
public function onClose(ConnectionInterface $connection)
26+
{
27+
// TODO: Implement onClose() method.
28+
}
29+
30+
public function onError(ConnectionInterface $connection, \Exception $e)
31+
{
32+
// TODO: Implement onError() method.
33+
}
34+
35+
public function onMessage(ConnectionInterface $connection, MessageInterface $msg)
36+
{
37+
// TODO: Implement onMessage() method.
38+
}
39+
}
40+
```
41+
42+
In the class itself you have full control over all the lifecycle events of your WebSocket connections and can intercept the incoming messages and react to them.
43+
44+
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.
45+
46+
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.
47+
48+
This could, for example, be done inside your `routes/web.php` file.
49+
50+
```php
51+
WebSocketsRouter::webSocket('/my-websocket', \App\MyCustomWebSocketHandler::class);
52+
```
53+
54+
Once you've added the custom WebSocket route, be sure to restart our WebSocket server for the changes to take place.

docs/basic-usage/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Basic Usage
3+
order: 2
4+
---

docs/basic-usage/pusher.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: Pusher Replacement
3+
order: 1
4+
---
5+
6+
# Pusher Replacement
7+
8+
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.
9+
10+
## Requirements
11+
12+
To make use of the Laravel WebSockets package in combination with Pusher, you first need to install the official Pusher PHP SDK.
13+
14+
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).
15+
16+
```bash
17+
composer require pusher/pusher-php-server "~3.0"
18+
```
19+
20+
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:
21+
22+
```
23+
BROADCAST_DRIVER=pusher
24+
```
25+
26+
## Pusher Configuration
27+
28+
When broadcasting events from your Laravel application to your WebSocket server, the default behavior is to send the event information to the official Pusher server. But since the Laravel WebSockets package comes with its own Pusher API implementation, we need to tell Laravel to send the events to our own server.
29+
30+
To do this, you should add the `host` and `port` configuration key to your `config/broadcasting.php` and add it to the `pusher` section. The default port of the Laravel WebSocket server is 6001.
31+
32+
```php
33+
'pusher' => [
34+
'driver' => 'pusher',
35+
'key' => env('PUSHER_APP_KEY'),
36+
'secret' => env('PUSHER_APP_SECRET'),
37+
'app_id' => env('PUSHER_APP_ID'),
38+
'options' => [
39+
'cluster' => env('PUSHER_APP_CLUSTER'),
40+
'encrypted' => true,
41+
'host' => '127.0.0.1',
42+
'port' => 6001,
43+
'scheme' => 'http'
44+
],
45+
],
46+
```
47+
48+
## Configuring WebSocket Apps
49+
50+
The Laravel WebSocket Pusher replacement server comes with multi-tenancy support out of the box. This means that you could host it independently from your current Laravel application and serve multiple WebSocket applications with one server.
51+
52+
To make the move from an existing Pusher setup to this package as easy as possible, the default app simply uses your existing Pusher configuration.
53+
54+
::: warning
55+
Make sure to use the same app id, key and secret as in your broadcasting configuration section. Otherwise broadcasting events from Laravel will not work.
56+
:::
57+
58+
::: tip
59+
When using Laravel WebSockets as a Pusher replacement without having used Pusher before, it does not matter what you set as your `PUSHER_` variables. Just make sure they are unique for each project.
60+
:::
61+
62+
You may add additional apps in your `config/websockets.php` file.
63+
64+
```php
65+
'apps' => [
66+
[
67+
'id' => env('PUSHER_APP_ID'),
68+
'name' => env('APP_NAME'),
69+
'key' => env('PUSHER_APP_KEY'),
70+
'secret' => env('PUSHER_APP_SECRET'),
71+
'enable_client_messages' => false,
72+
'enable_statistics' => true,
73+
],
74+
],
75+
```
76+
77+
### Client Messages
78+
79+
For each app in your configuration file, you can define if this specific app should support a client-to-client messages. Usually all WebSocket messages go through your Laravel application before they will be broadcasted to other users. But sometimes you may want to enable a direct client-to-client communication instead of sending the events over the server. For example, a "typing" event in a chat application.
80+
81+
It is important that you apply additional care when using client messages, since these originate from other users, and could be subject to tampering by a malicious user of your site.
82+
83+
To enable or disable client messages, you can modify the `enable_client_messages` setting. The default value is `false`.
84+
85+
### Statistics
86+
87+
The Laravel WebSockets package comes with an out-of-the-box statistic solution that will give you key insights into the current status of your WebSocket server.
88+
89+
To enable or disable the statistics for one of your apps, you can modify the `enable_statistics` setting. The default value is `true`.
90+
91+
## Usage with Laravel Echo
92+
93+
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.
94+
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).
95+
96+
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.
97+
98+
By default, the Pusher JavaScript client tries to send statistic information - you should disable this using the `disableStats` option.
99+
100+
::: tip
101+
When using Laravel WebSockets in combination with a custom SSL certificate, be sure to use the `encrypted` option and set it to `true`.
102+
:::
103+
104+
```js
105+
import Echo from "laravel-echo"
106+
107+
window.Pusher = require('pusher-js');
108+
109+
window.Echo = new Echo({
110+
broadcaster: 'pusher',
111+
key: 'your-pusher-key',
112+
wsHost: window.location.hostname,
113+
wsPort: 6001,
114+
disableStats: true,
115+
});
116+
```
117+
118+
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).

0 commit comments

Comments
 (0)