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

Commit aa1c11e

Browse files
authored
Merge branch 'master' into redis-replication
2 parents db58378 + 4c12fb3 commit aa1c11e

File tree

73 files changed

+1218
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1218
-180
lines changed

.github/workflows/run-tests.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: run-tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
os: [ubuntu-latest, windows-latest]
12+
php: [7.4, 7.3, 7.2]
13+
laravel: [5.8.*, 6.*, 7.*]
14+
dependency-version: [prefer-lowest, prefer-stable]
15+
include:
16+
- laravel: 7.*
17+
testbench: 5.*
18+
- laravel: 6.*
19+
testbench: 4.*
20+
- laravel: 5.8.*
21+
testbench: 3.8.*
22+
23+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
24+
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v1
28+
29+
- name: Cache dependencies
30+
uses: actions/cache@v1
31+
with:
32+
path: ~/.composer/cache/files
33+
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
34+
35+
- name: Setup PHP
36+
uses: shivammathur/setup-php@v2
37+
with:
38+
php-version: ${{ matrix.php }}
39+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
40+
coverage: none
41+
42+
- name: Install dependencies
43+
run: |
44+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
45+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
46+
47+
- name: Execute tests
48+
run: vendor/bin/phpunit

.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

.travis.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-websockets` will be documented in this file
44

5+
## 1.4.0 - 2020-03-03
6+
7+
- add support for Laravel 7
8+
59
## 1.0.2 - 2018-12-06
610

711
- Fix issue with wrong namespaces

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Laravel WebSockets 🛰
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-websockets.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-websockets)
4-
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-websockets/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-websockets)
4+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/beyondcode/laravel-websockets/run-tests?label=tests)
55
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-websockets.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-websockets)
66
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-websockets.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-websockets)
77

composer.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@
2222
}
2323
],
2424
"require": {
25-
"php": "^7.1",
25+
"php": "^7.2",
2626
"ext-json": "*",
2727
"cboden/ratchet": "^0.4.1",
2828
"clue/buzz-react": "^2.5",
2929
"clue/redis-react": "^2.3",
3030
"facade/ignition-contracts": "^1.0",
3131
"guzzlehttp/psr7": "^1.5",
32-
"illuminate/broadcasting": "5.7.* || 5.8.* || ^6.0",
33-
"illuminate/console": "5.7.* || 5.8.* || ^6.0",
34-
"illuminate/http": "5.7.* || 5.8.* || ^6.0",
35-
"illuminate/routing": "5.7.* || 5.8.* || ^6.0",
36-
"illuminate/support": "5.7.* || 5.8.* || ^6.0",
37-
"pusher/pusher-php-server": "~3.0 || ~4.0",
32+
"illuminate/broadcasting": "5.8.*|^6.0|^7.0",
33+
"illuminate/console": "5.8.*|^6.0|^7.0",
34+
"illuminate/http": "5.8.*|^6.0|^7.0",
35+
"illuminate/routing": "5.8.*|^6.0|^7.0",
36+
"illuminate/support": "5.8.*|^6.0|^7.0",
37+
"pusher/pusher-php-server": "^3.0|^4.0",
3838
"react/dns": "^1.1",
39-
"symfony/http-kernel": "~4.0",
40-
"symfony/psr-http-message-bridge": "^1.1"
39+
"symfony/http-kernel": "^4.0|^5.0",
40+
"symfony/psr-http-message-bridge": "^1.1|^2.0"
4141
},
4242
"require-dev": {
43-
"mockery/mockery": "^1.2",
44-
"orchestra/testbench": "3.7.* || 3.8.* || ^4.0",
45-
"phpunit/phpunit": "^7.0 || ^8.0"
43+
"mockery/mockery": "^1.3",
44+
"orchestra/testbench": "3.8.*|^4.0|^5.0",
45+
"phpunit/phpunit": "^8.0|^9.0"
4646
},
4747
"autoload": {
4848
"psr-4": {

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.

0 commit comments

Comments
 (0)