1
<?php
2

3
namespace Nuwave\Lighthouse\Subscriptions\Broadcasters;
4

5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Http\Request;
7
use Nuwave\Lighthouse\Subscriptions\Contracts\Broadcaster;
8
use Nuwave\Lighthouse\Subscriptions\Contracts\StoresSubscriptions;
9
use Nuwave\Lighthouse\Subscriptions\Subscriber;
10
use Pusher\Pusher;
11

12
class PusherBroadcaster implements Broadcaster
13
{
14
    public const EVENT_NAME = 'lighthouse-subscription';
15

16
    /**
17
     * @var \Pusher\Pusher
18
     */
19
    protected $pusher;
20

21
    /**
22
     * @var \Nuwave\Lighthouse\Subscriptions\Contracts\StoresSubscriptions
23
     */
24
    protected $storage;
25

26 1
    public function __construct(Pusher $pusher)
27
    {
28 1
        $this->pusher = $pusher;
29 1
        $this->storage = app(StoresSubscriptions::class);
30
    }
31

32 0
    public function authorized(Request $request): JsonResponse
33
    {
34 0
        $channel = $request->input('channel_name');
35 0
        $socketId = $request->input('socket_id');
36 0
        $data = \Safe\json_decode(
37 0
            $this->pusher->socket_auth($channel, $socketId),
38 0
            true
39
        );
40

41 0
        return new JsonResponse($data, 200);
42
    }
43

44 0
    public function unauthorized(Request $request): JsonResponse
45
    {
46 0
        return new JsonResponse([
47 0
            'error' => 'unauthorized',
48 0
        ], 403);
49
    }
50

51 0
    public function hook(Request $request): JsonResponse
52
    {
53 0
        foreach ($request->input('events', []) as $event) {
54 0
            if ($event['name'] === 'channel_vacated') {
55 0
                $this->storage->deleteSubscriber($event['channel']);
56
            }
57
        }
58

59 0
        return new JsonResponse(['message' => 'okay']);
60
    }
61

62 0
    public function broadcast(Subscriber $subscriber, $data): void
63
    {
64 0
        $this->pusher->trigger(
65 0
            $subscriber->channel,
66 0
            self::EVENT_NAME,
67
            [
68 0
                'more' => true,
69 0
                'result' => $data,
70
            ]
71
        );
72
    }
73
}

Read our documentation on viewing source code .

Loading