Skip to content

Restructure Broadcasting docs #44

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

Merged
merged 8 commits into from
Sep 16, 2024
Merged
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
104 changes: 40 additions & 64 deletions resources/views/docs/1/digging-deeper/broadcasting.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,97 +5,73 @@ order: 100

# Broadcasting

NativePHP fires various events during its operations. You may listen for these events using any event listener in your
application as you normally would.
NativePHP facilitates event broadcasting of both [native events](#native-events) (emitted by Electron/Tauri) and
[custom events](#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](#listening-with-javascript) on your pages.

## Reacting to NativePHP events in real-time
## Native events

In order to react to NativePHP events as they happen, such as focusing a Window, NativePHP broadcasts these events in two different ways:
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`](https://github.com/nativephp/laravel/tree/main/src/Events) folder.

### WebSockets
NativePHP broadcasts events over a websocket connection on the `nativephp` broadcast channel.
This allows you to listen for these events in real-time using Laravel Echo and react to these events on your application's
front-end or via Laravel Livewire.
## Custom events

In order to make use of WebSockets in your NativePHP application, you can install the [Laravel WebSockets](https://beyondco.de/docs/laravel-websockets) package in your Laravel app.
NativePHP automatically runs `php artisan websockets:serve` to start your WebSocket server once your application runs, allowing you to use Laravel Echo to connect to it and listen for events in real-time.
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:

### Browser Events
```php
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

In addition to connecting to a local WebSocket server using Laravel Echo, you can also listen to all internal PHP events in real-time using JavaScript by making use of Electron's [Inter-Process-Communication System](https://electronjs.org/docs/latest/api/ipc-renderer).
This is especially useful as it reduces the overhead and latency of dispatching and listening Laravel events via a WebSocket connection.
class JobFinished implements ShouldBroadcastNow
{
public function broadcastOn(): array
{
return [
new Channel('nativephp'),
];
}
}
```

### JavaScript
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.

You can listen to these events in real-time by making use of Electron's `ipcRenderer` API.
This API dispatches a `native-event` event, that contains the following payload:
## Listening with JavaScript

* `event` - A string containing the fully qualified class name of the NativePHP event class that got dispatched
* `payload` - An object that contains the event payload
You can listen to all native and custom events emitted by your application in real-time using JavaScript.

```js
import * as remote from '@electron/remote'
NativePHP injecst 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:

ipcRenderer.on('native-event', (_, data) => {
const eventClassName = data.event;
const eventPayload = data.payload;
```js
Native.on("Native\\Laravel\\Events\\Windows\\WindowBlurred", (payload, event) => {
//
});
```

### Livewire
## Listening with Livewire

To make this process even easier when using [Livewire](https://laravel-livewire.com), you may use the `native:` prefix when listening to events.
This is similar to [listening to Laravel Echo events using Livewire](https://laravel-livewire.com/docs/2.x/laravel-echo).
To make this process even easier when using [Livewire](https://livewire.laravel.com), you may use the `native:` prefix when
listening to events. This is similar to
[listening to Laravel Echo events using Livewire](https://livewire.laravel.com/docs/events#real-time-events-using-laravel-echo).

```php
class AppSettings extends Component
{
public $windowFocused = true;

// Special Syntax: ['native:{event}' => '{method}']
protected $listeners = [
'native:'.\Native\Laravel\Events\Windows\WindowFocused::class => 'windowFocused',
'native:'.\Native\Laravel\Events\Windows\WindowBlurred::class => 'windowBlurred',
];


#[On('native:\Native\Laravel\Events\Windows\WindowFocused')]
public function windowFocused()
{
$this->windowFocused = true;
}


#[On('native:\Native\Laravel\Events\Windows\WindowBlurred')]
public function windowBlurred()
{
$this->windowFocused = false;
}
}
```

## Available Events

A full list of all events fired and broadcast by NativePHP can be found in the
[src/Events/](https://github.com/nativephp/laravel/tree/main/src/Events) folder.

## Broadcasting custom events

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

```php
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

class JobFinished implements ShouldBroadcastNow
{
public function broadcastOn(): array
{
return [
new Channel('nativephp'),
];
}
}
```

This is great for times when you want to offload an intensive task to a background queue and await its completion
without constantly polling your application for its status.

Your fired event will be broadcast and your application can listen for it and just as your normally would.