Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Broadcast custom Events on IPC #367

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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