Understanding NativePHP Desktop’s event system and event broadcasting
NativePHP Desktop uses Laravel’s event system to enable communication between your PHP application and the native runtime. Events are automatically broadcast between PHP and Electron, creating a seamless bridge for reactive applications.
To broadcast custom events to the native runtime, implement ShouldBroadcastNow and broadcast on the nativephp channel:
1
Create the Event
namespace App\Events;use Illuminate\Broadcasting\Channel;use Illuminate\Broadcasting\InteractsWithSockets;use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;use Illuminate\Foundation\Events\Dispatchable;use Illuminate\Queue\SerializesModels;class TaskCompleted implements ShouldBroadcastNow{ use Dispatchable, InteractsWithSockets, SerializesModels; public function __construct( public string $taskName, public string $status ) {} public function broadcastOn() { return [new Channel('nativephp')]; }}
2
Dispatch the Event
use App\Events\TaskCompleted;TaskCompleted::dispatch('Process Data', 'success');
3
Event is Automatically Broadcast
The EventWatcher automatically sends the event to the native runtime, where you can listen for it in JavaScript.
Only events that broadcast on the nativephp channel are sent to the native runtime. Events on other channels are ignored.
use Native\Desktop\Events\Windows\WindowFocused;Event::listen(WindowFocused::class, function ($event) { Log::info('Window focused: ' . $event->id);});
Event structure:
class WindowFocused implements ShouldBroadcastNow{ use Dispatchable, InteractsWithSockets, SerializesModels; public function __construct(public string $id) {} public function broadcastOn() { return [new Channel('nativephp')]; }}
use Native\Desktop\Events\Windows\WindowClosed;Event::listen(WindowClosed::class, function ($event) { Log::info('Window closed: ' . $event->id); // Cleanup resources Cache::forget('window_state_' . $event->id);});
Available window events:
WindowShown
WindowHidden
WindowMinimized
WindowMaximized
WindowResized
WindowBlurred
All window events include the window id:
Event::listen(WindowResized::class, function ($event) { echo $event->id; // 'main'});
use Native\Desktop\Events\Menu\MenuItemClicked;Event::listen(MenuItemClicked::class, function ($event) { $item = $event->item; // Menu item data $combo = $event->combo; // Keyboard shortcut used Log::info('Menu clicked: ' . $item['label']); match($item['label']) { 'Preferences' => redirect()->route('settings'), 'About' => Window::open('about'), default => null, };});
Event structure:
class MenuItemClicked implements ShouldBroadcastNow{ use Dispatchable, InteractsWithSockets, SerializesModels; public function __construct( public array $item, public array $combo = [] ) {} public function broadcastOn() { return [new Channel('nativephp')]; }}
use Native\Desktop\Events\App\ApplicationBooted;use Native\Desktop\Events\App\OpenFile;use Native\Desktop\Events\App\OpenedFromURL;// Application finished bootingEvent::listen(ApplicationBooted::class, function () { Log::info('NativePHP application booted');});// User opened a file with the appEvent::listen(OpenFile::class, function ($event) { $filePath = $event->path; // Handle file opening});// App opened from custom URL schemeEvent::listen(OpenedFromURL::class, function ($event) { $url = $event->url; // Handle deep linking});
use Native\Desktop\Events\PowerMonitor\PowerStateChanged;use Native\Desktop\Events\PowerMonitor\ScreenLocked;use Native\Desktop\Events\PowerMonitor\ScreenUnlocked;use Native\Desktop\Events\PowerMonitor\Shutdown;use Native\Desktop\Events\PowerMonitor\ThermalStateChanged;use Native\Desktop\Events\PowerMonitor\UserDidBecomeActive;use Native\Desktop\Events\PowerMonitor\UserDidResignActive;// Monitor battery/AC power changesEvent::listen(PowerStateChanged::class, function ($event) { if ($event->isOnBattery) { // Switch to power-saving mode }});// Screen lockedEvent::listen(ScreenLocked::class, function () { // Pause sensitive operations});// Screen unlockedEvent::listen(ScreenUnlocked::class, function () { // Resume operations});// System shutting downEvent::listen(Shutdown::class, function () { // Save state before shutdown});// User became active/inactiveEvent::listen(UserDidBecomeActive::class, function () { // User returned});Event::listen(UserDidResignActive::class, function () { // User switched away});
use Native\Desktop\Events\AutoUpdater\CheckingForUpdate;use Native\Desktop\Events\AutoUpdater\UpdateAvailable;use Native\Desktop\Events\AutoUpdater\UpdateNotAvailable;use Native\Desktop\Events\AutoUpdater\DownloadProgress;use Native\Desktop\Events\AutoUpdater\UpdateDownloaded;use Native\Desktop\Events\AutoUpdater\Error;Event::listen(CheckingForUpdate::class, function () { // Show "checking for updates" UI});Event::listen(UpdateAvailable::class, function ($event) { Notification::new() ->title('Update Available') ->message('Version ' . $event->version . ' is available') ->show();});Event::listen(DownloadProgress::class, function ($event) { $percent = $event->percent; // Update progress bar});Event::listen(UpdateDownloaded::class, function () { // Prompt user to restart});
use Native\Desktop\Events\MenuBar\MenuBarClicked;use Native\Desktop\Events\MenuBar\MenuBarDoubleClicked;use Native\Desktop\Events\MenuBar\MenuBarRightClicked;use Native\Desktop\Events\MenuBar\MenuBarDroppedFiles;Event::listen(MenuBarClicked::class, function () { // Single click on menu bar icon});Event::listen(MenuBarRightClicked::class, function () { // Right/context click});Event::listen(MenuBarDroppedFiles::class, function ($event) { $files = $event->files; // Handle dropped files});
use Illuminate\Support\Facades\Event;use Native\Desktop\Events\Windows\WindowFocused;Event::listen(WindowFocused::class, function ($event) { Cache::put('last_focused_window', $event->id);});
Not be in the Native\Desktop\Events namespace (to avoid loops)
use Illuminate\Broadcasting\Channel;use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;class MyCustomEvent implements ShouldBroadcastNow{ public function broadcastOn() { return [new Channel('nativephp')]; }}
Use ShouldBroadcastNow instead of ShouldBroadcast to ensure events are sent immediately without queue delays.