Skip to content

Commit 91afa37

Browse files
authored
Feature: Broadcast custom Events on IPC (#367)
* Add EventWatcher * Add event name to client request * Filter only custom events & fix event fqcn
1 parent 405691a commit 91afa37

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Events/EventWatcher.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events;
4+
5+
use Illuminate\Support\Facades\Event;
6+
use Native\Laravel\Client\Client;
7+
8+
class EventWatcher
9+
{
10+
public function __construct(protected Client $client) {}
11+
12+
public function register(): void
13+
{
14+
Event::listen('*', function (string $eventName, array $data) {
15+
16+
$event = $data[0] ?? null;
17+
18+
if (! method_exists($event, 'broadcastOn')) {
19+
return;
20+
}
21+
22+
$channels = $event->broadcastOn();
23+
24+
// Only events dispatched on the nativephp channel
25+
if(! in_array('nativephp', $channels)) {
26+
return;
27+
}
28+
29+
// Only post custom events to broadcasting endpoint
30+
if(str_starts_with($eventName ,'Native\\Laravel\\Events')) {
31+
return;
32+
}
33+
34+
$this->client->post('broadcast', [
35+
'event' => "\\{$eventName}",
36+
'payload' => $event
37+
]);
38+
});
39+
}
40+
}

src/NativeServiceProvider.php

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Native\Laravel\Commands\MinifyApplicationCommand;
1212
use Native\Laravel\Commands\SeedDatabaseCommand;
1313
use Native\Laravel\Logging\LogWatcher;
14+
use Native\Laravel\Events\EventWatcher;
1415
use Spatie\LaravelPackageTools\Package;
1516
use Spatie\LaravelPackageTools\PackageServiceProvider;
1617

@@ -61,6 +62,8 @@ protected function configureApp()
6162
app(LogWatcher::class)->register();
6263
}
6364

65+
app(EventWatcher::class)->register();
66+
6467
$this->rewriteStoragePath();
6568

6669
$this->rewriteDatabase();

0 commit comments

Comments
 (0)