-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Broadcast custom Events on IPC (#367)
* Add EventWatcher * Add event name to client request * Filter only custom events & fix event fqcn
- Loading branch information
1 parent
405691a
commit 91afa37
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters