Skip to content

Commit

Permalink
broadcasting channels registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Landori committed Jan 25, 2024
1 parent 7ef30cc commit ef1507c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 3 deletions.
24 changes: 24 additions & 0 deletions Channels/FinderChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Channels;

use Illuminate\Foundation\Auth\User;

class FinderChannel
{
/**
* Create a new channel instance.
*/
public function __construct()
{
//
}

/**
* Authenticate the user's access to the channel.
*/
public function join(User $user, int $id): bool
{
return $user->id === $id;
}
}
6 changes: 6 additions & 0 deletions routes/channels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use Illuminate\Support\Facades\Broadcast;
use App\Channels\FinderChannel;

Broadcast::channel(config('finder.broadcasting.channel_name') . '.{id}', FinderChannel::class);
10 changes: 8 additions & 2 deletions src/Events/SearchResultFoundBroadcastEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class SearchResultFoundBroadcastEvent extends BroadcastNowEvent {
*/
public function __construct(string $type, string $message = '', array $data = []) {
parent::__construct($type, $message, $data);

var_dump('broadcasting ' . $message . ' as ' . $this->broadcastAs() . ' on ' . json_encode($this->broadcastOn()));
}

/**
Expand All @@ -33,9 +35,13 @@ public function broadcastOn() {
$channelName = config('finder.broadcasting.channel_name', 'finder.results');

if( $channelType === 'public' || ! Auth::user() ){
return new Channel($channelName);
return [
new Channel($channelName)
];
}

return new PrivateChannel("{$channelName}." . auth()->user()->id);
return [
new PrivateChannel("{$channelName}." . auth()->user()->id)
];
}
}
9 changes: 9 additions & 0 deletions src/FinderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use link0\Finder\Drivers\DriverRegistry;
use link0\Finder\Interfaces\FinderInterface;
use link0\Finder\Providers\EventServiceProvider;
use link0\Finder\Providers\BroadcastServiceProvider;

class FinderServiceProvider extends ServiceProvider {
/**
Expand Down Expand Up @@ -36,6 +37,9 @@ public function register() {

// register events
$this->app->register(EventServiceProvider::class);

// register channels
$this->app->register(BroadcastServiceProvider::class);
}

/**
Expand All @@ -52,8 +56,13 @@ public function boot() {

$this->publishes([
__DIR__.'/../routes/web.php' => base_path('routes/finder.php'),
__DIR__.'/../routes/channels.php' => base_path('routes/channels_finder.php'),
], 'routes');

$this->publishes([
__DIR__.'/../Channels/FinderChannel.php' => base_path('app/Channels/FinderChannel.php'),
], 'channels');

$this->commands([
InstallFinder::class,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/SearchResultFoundListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function handle(SearchResultFoundEvent $event) {
$broadcastMethod = config('finder.broadcasting.method');

if ($broadcastMethod === 'websockets') {
event(new SearchResultFoundBroadcastEvent($event->type, $event->message, $event->data));
broadcast(new SearchResultFoundBroadcastEvent($event->type, $event->message, $event->data));
}
}
}
26 changes: 26 additions & 0 deletions src/Providers/BroadcastServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace link0\Finder\Providers;

use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;

class BroadcastServiceProvider extends ServiceProvider {
/**
* Bootstrap any application services.
*/
public function boot(): void {
Broadcast::routes();

$this->registerChannels();
}

private function registerChannels(){
$channelsFile = base_path('routes/channels_finder.php');

if( File::exists($channelsFile) ){
require $channelsFile;
}
}
}

0 comments on commit ef1507c

Please sign in to comment.