Skip to content

Commit

Permalink
Feature: Broadcast custom Events on IPC (#367)
Browse files Browse the repository at this point in the history
* Add EventWatcher

* Add event name to client request

* Filter only custom events & fix event fqcn
  • Loading branch information
gwleuverink authored Sep 10, 2024
1 parent 405691a commit 91afa37
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Events/EventWatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Native\Laravel\Events;

use Illuminate\Support\Facades\Event;
use Native\Laravel\Client\Client;

class EventWatcher
{
public function __construct(protected Client $client) {}

public function register(): void
{
Event::listen('*', function (string $eventName, array $data) {

$event = $data[0] ?? null;

if (! method_exists($event, 'broadcastOn')) {
return;
}

$channels = $event->broadcastOn();

// Only events dispatched on the nativephp channel
if(! in_array('nativephp', $channels)) {
return;
}

// Only post custom events to broadcasting endpoint
if(str_starts_with($eventName ,'Native\\Laravel\\Events')) {
return;
}

$this->client->post('broadcast', [
'event' => "\\{$eventName}",
'payload' => $event
]);
});
}
}
3 changes: 3 additions & 0 deletions src/NativeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Native\Laravel\Commands\MinifyApplicationCommand;
use Native\Laravel\Commands\SeedDatabaseCommand;
use Native\Laravel\Logging\LogWatcher;
use Native\Laravel\Events\EventWatcher;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand Down Expand Up @@ -61,6 +62,8 @@ protected function configureApp()
app(LogWatcher::class)->register();
}

app(EventWatcher::class)->register();

$this->rewriteStoragePath();

$this->rewriteDatabase();
Expand Down

0 comments on commit 91afa37

Please sign in to comment.