You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 7, 2024. It is now read-only.
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
9
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.
6
11
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.
8
13
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.
Copy file name to clipboardExpand all lines: docs/advanced-usage/custom-websocket-handlers.md
+8-3Lines changed: 8 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,11 @@
1
+
---
2
+
title: Custom WebSocket Handlers
3
+
order: 2
4
+
---
5
+
1
6
# Custom WebSocket Handlers
2
7
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.
4
9
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
10
6
11
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
21
26
{
22
27
// TODO: Implement onOpen() method.
23
28
}
24
-
29
+
25
30
public function onClose(ConnectionInterface $connection)
26
31
{
27
32
// TODO: Implement onClose() method.
@@ -51,4 +56,4 @@ This could, for example, be done inside your `routes/web.php` file.
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:
Copy file name to clipboardExpand all lines: docs/basic-usage/pusher.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,14 +7,16 @@ order: 1
7
7
8
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
9
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
+
10
12
## Requirements
11
13
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.
13
15
14
16
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
17
16
18
```bash
17
-
composer require pusher/pusher-php-server "~3.0"
19
+
composer require pusher/pusher-php-server "~4.0"
18
20
```
19
21
20
22
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
40
42
'encrypted' => true,
41
43
'host' => '127.0.0.1',
42
44
'port' => 6001,
43
-
'scheme' => 'http'
45
+
'scheme' => 'http',
44
46
],
45
47
],
46
48
```
@@ -68,6 +70,8 @@ You may add additional apps in your `config/websockets.php` file.
68
70
'name' => env('APP_NAME'),
69
71
'key' => env('PUSHER_APP_KEY'),
70
72
'secret' => env('PUSHER_APP_SECRET'),
73
+
'path' => env('PUSHER_APP_PATH'),
74
+
'capacity' => null,
71
75
'enable_client_messages' => false,
72
76
'enable_statistics' => true,
73
77
],
@@ -113,7 +117,8 @@ window.Echo = new Echo({
113
117
wsPort:6001,
114
118
forceTLS:false,
115
119
disableStats:true,
120
+
enabledTransports: ['ws', 'wss'],
116
121
});
117
122
```
118
123
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).
0 commit comments