|
| 1 | +--- |
| 2 | +title: Broadcasting |
| 3 | +order: 100 |
| 4 | +--- |
| 5 | + |
| 6 | +# Broadcasting |
| 7 | + |
| 8 | +NativePHP facilitates event broadcasting of both [native events](#native-events) (emitted by Electron/Tauri) and |
| 9 | +[custom events](#custom-events) dispatched by your Laravel app. You can listen to all of these events in your |
| 10 | +Laravel application as you normally would or in the [JavaScript](#listening-with-javascript) on your pages. |
| 11 | + |
| 12 | +## Native events |
| 13 | + |
| 14 | +NativePHP fires various events during its operations, such as `WindowBlurred` & `NotificationClicked`. A full list |
| 15 | +of all events fired and broadcast by NativePHP can be found in the |
| 16 | +[`src/Events`](https://github.com/nativephp/laravel/tree/main/src/Events) folder. |
| 17 | + |
| 18 | +## Custom events |
| 19 | + |
| 20 | +You can also broadcast your own events. Simply implement the `ShouldBroadcastNow` contract in your event class and |
| 21 | +define the `broadcastOn` method, returning `nativephp` as one of the channels it broadcasts to: |
| 22 | + |
| 23 | +```php |
| 24 | +use Illuminate\Broadcasting\Channel; |
| 25 | +use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; |
| 26 | + |
| 27 | +class JobFinished implements ShouldBroadcastNow |
| 28 | +{ |
| 29 | + public function broadcastOn(): array |
| 30 | + { |
| 31 | + return [ |
| 32 | + new Channel('nativephp'), |
| 33 | + ]; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +This is particularly useful for scenarios where you want to offload an intensive task to a background queue and await |
| 39 | +its completion without constantly polling your application for its status. |
| 40 | + |
| 41 | +## Listening with JavaScript |
| 42 | + |
| 43 | +You can listen to all native and custom events emitted by your application in real-time using JavaScript. |
| 44 | + |
| 45 | +NativePHP injects a `window.Native` object into every window. The `on()` method allows you to register a callback as |
| 46 | +the second parameter that will run when the event specified in the first parameter is fired: |
| 47 | + |
| 48 | +```js |
| 49 | +Native.on("Native\\Laravel\\Events\\Windows\\WindowBlurred", (payload, event) => { |
| 50 | + // |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +## Listening with Livewire |
| 55 | + |
| 56 | +To make this process even easier when using [Livewire](https://livewire.laravel.com), you may use the `native:` prefix when |
| 57 | +listening to events. This is similar to |
| 58 | +[listening to Laravel Echo events using Livewire](https://livewire.laravel.com/docs/events#real-time-events-using-laravel-echo). |
| 59 | + |
| 60 | +You may use a string name: |
| 61 | + |
| 62 | +```php |
| 63 | +class AppSettings extends Component |
| 64 | +{ |
| 65 | + public $windowFocused = true; |
| 66 | + |
| 67 | + #[On('native:\\Native\\Laravel\\Events\\Windows\\WindowFocused')] |
| 68 | + public function windowFocused() |
| 69 | + { |
| 70 | + $this->windowFocused = true; |
| 71 | + } |
| 72 | + |
| 73 | + #[On('native:\\Native\\Laravel\\Events\\Windows\\WindowBlurred')] |
| 74 | + public function windowBlurred() |
| 75 | + { |
| 76 | + $this->windowFocused = false; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +You may find it more convenient to use PHP's class name resolution keyword, `::class`: |
| 82 | + |
| 83 | +```php |
| 84 | +use Native\Laravel\Events\Windows\WindowBlurred; |
| 85 | +use Native\Laravel\Events\Windows\WindowFocused; |
| 86 | + |
| 87 | +class AppSettings extends Component |
| 88 | +{ |
| 89 | + public $windowFocused = true; |
| 90 | + |
| 91 | + #[On('native:'.WindowFocused::class)] |
| 92 | + public function windowFocused() |
| 93 | + { |
| 94 | + $this->windowFocused = true; |
| 95 | + } |
| 96 | + |
| 97 | + #[On('native:'.WindowBlurred::class)] |
| 98 | + public function windowBlurred() |
| 99 | + { |
| 100 | + $this->windowFocused = false; |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
0 commit comments