NativePHP
Windows support is here! 🔥 Read the full announcement →
Broadcasting

NativePHP is currently in alpha development

Let's get to beta!

#
Broadcasting

NativePHP facilitates event broadcasting of both native events (emitted by Electron/Tauri) and custom events dispatched by your Laravel app. You can listen to all of these events in your Laravel application as you normally would or in the JavaSscript on your pages.

#
Native events

NativePHP fires various events during its operations, such as WindowBlurred & NotificationClicked. A full list of all events fired and broadcast by NativePHP can be found in the src/Events folder.

#
Custom events

You can also broadcast your own events. Simply implement the ShouldBroadcastNow contract in your event class and define the broadcastOn method, returning nativephp as one of the channels it broadcasts to:

Copied!
1use Illuminate\Broadcasting\Channel;
2use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
3 
4class JobFinished implements ShouldBroadcastNow
5{
6 public function broadcastOn(): array
7 {
8 return [
9 new Channel('nativephp'),
10 ];
11 }
12}

This is particularly useful for scenarios where you want to offload an intensive task to a background queue and await its completion without constantly polling your application for its status.

#
Listening with JavaScript

You can listen to all native and custom events emitted by your application in real-time using JavaScript.

NativePHP injects a window.Native object into every window. The on() method allows you to register a callback as the second parameter that will run when the event specified in the first parameter is fired:

Copied!
1Native.on("Native\\Laravel\\Events\\Windows\\WindowBlurred", (payload, event) => {
2 //
3});

#
Listening with Livewire

To make this process even easier when using Livewire, you may use the native: prefix when listening to events. This is similar to listening to Laravel Echo events using Livewire.

You may use a string name:

Copied!
1class AppSettings extends Component
2{
3 public $windowFocused = true;
4 
5 #[On('native:\\Native\\Laravel\\Events\\Windows\\WindowFocused')]
6 public function windowFocused()
7 {
8 $this->windowFocused = true;
9 }
10 
11 #[On('native:\\Native\\Laravel\\Events\\Windows\\WindowBlurred')]
12 public function windowBlurred()
13 {
14 $this->windowFocused = false;
15 }
16}

You may find it more convenient to use PHP's class name resolution keyword, ::class:

Copied!
1use Native\Laravel\Events\Windows\WindowBlurred;
2use Native\Laravel\Events\Windows\WindowFocused;
3 
4class AppSettings extends Component
5{
6 public $windowFocused = true;
7 
8 #[On('native:'.WindowFocused::class)]
9 public function windowFocused()
10 {
11 $this->windowFocused = true;
12 }
13 
14 #[On('native:'.WindowBlurred::class)]
15 public function windowBlurred()
16 {
17 $this->windowFocused = false;
18 }
19}