Skip to content

Commit 389606f

Browse files
authored
Pusher Authorization Endpoint (#16)
* added pusher auth endpoint. * Fix styling
1 parent 9e1a23e commit 389606f

File tree

5 files changed

+263
-6
lines changed

5 files changed

+263
-6
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": "^8.0",
2020
"illuminate/contracts": "^8.0|^9.0",
21-
"guzzlehttp/guzzle": "^7.4.5"
21+
"guzzlehttp/guzzle": "^7.4.5",
22+
"pusher/pusher-php-server": "^7.1@beta"
2223
},
2324
"require-dev": {
2425
"ext-pdo_sqlite": "*",

composer.lock

Lines changed: 206 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/BroadcastAuthRoute.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Illuminate\Http\Request;
4+
use Illuminate\Support\Facades\Route;
5+
use YorCreative\QueryWatcher\Services\BroadcastAuthService;
6+
7+
Route::post('/broadcasting/query-watcher/{driver}/auth', function (Request $request, $driver) {
8+
return match ($driver) {
9+
'pusher' => BroadcastAuthService::pusher($request),
10+
default => response([], 401),
11+
};
12+
})->name('broadcasting.pusher.auth');

src/QueryWatcherServiceProvider.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace YorCreative\QueryWatcher;
44

5-
use Illuminate\Support\Facades\Broadcast;
5+
use Illuminate\Support\Facades\Config;
66
use Illuminate\Support\Facades\Event;
77
use Illuminate\Support\ServiceProvider;
8+
use Pusher\Pusher;
89
use YorCreative\QueryWatcher\Events\QueryEvent;
910
use YorCreative\QueryWatcher\Listeners\QueryListener;
1011

@@ -20,6 +21,8 @@ public function register()
2021
{
2122
$this->mergeConfigFrom(dirname(__DIR__, 1).'/config/querywatcher.php', 'querywatcher');
2223

24+
$this->loadRoutesFrom(dirname(__DIR__, 1).'/routes/BroadcastAuthRoute.php');
25+
2326
$this->publishes([
2427
dirname(__DIR__, 1).'/config' => base_path('config'),
2528
]);
@@ -49,8 +52,14 @@ public function getEventListeners(): array
4952

5053
public function boot()
5154
{
52-
Broadcast::routes();
53-
5455
require dirname(__DIR__, 1).'/routes/QueryChannel.php';
56+
57+
$this->app->singleton(Pusher::class, function () {
58+
return new Pusher(
59+
Config::get('broadcasting.connections.pusher.key'),
60+
Config::get('broadcasting.connections.pusher.secret'),
61+
Config::get('broadcasting.connections.pusher.app_id')
62+
);
63+
});
5564
}
5665
}

src/Services/BroadcastAuthService.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace YorCreative\QueryWatcher\Services;
4+
5+
use Illuminate\Contracts\Foundation\Application;
6+
use Illuminate\Contracts\Routing\ResponseFactory;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Http\Response;
9+
use Pusher\Pusher;
10+
use Pusher\PusherException;
11+
12+
class BroadcastAuthService
13+
{
14+
/**
15+
* @param Request $request
16+
* @return Response|Application|ResponseFactory
17+
*
18+
* @throws PusherException
19+
*/
20+
public static function pusher(Request $request): Response|Application|ResponseFactory
21+
{
22+
$pusher = app(Pusher::class);
23+
24+
$auth = $pusher->authorizeChannel(
25+
$request->input('channel_name'),
26+
$request->input('socket_id')
27+
);
28+
29+
return response($auth, 200);
30+
}
31+
}

0 commit comments

Comments
 (0)