NativePHP for Mobile is now completely free and open source!

Events


#Dispatching Events from Native Code

Many native operations are asynchronous — ML inference, sensor readings, background tasks. Your native code needs a way to send results back to PHP when they're ready. That's where events come in.

Events are dispatched from native code and received by your Livewire components.

#Declaring Events

Add your event classes to the manifest:

Copied!
{
"events": [
"Vendor\\MyPlugin\\Events\\ProcessingComplete",
"Vendor\\MyPlugin\\Events\\ProcessingError"
]
}

#Creating Event Classes

Events are simple PHP classes:

Copied!
namespace Vendor\MyPlugin\Events;
 
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
 
class ProcessingComplete
{
use Dispatchable, SerializesModels;
 
public function __construct(
public string $result,
public ?string $id = null
) {}
}

#Swift Event Dispatching

Dispatch events using LaravelBridge.shared.send:

Copied!
// Build your payload
let payload: [String: Any] = [
"result": processedData,
"id": requestId
]
 
// Dispatch to PHP
LaravelBridge.shared.send?(
"Vendor\\MyPlugin\\Events\\ProcessingComplete",
payload
)

This runs synchronously on the main thread, so wrap in DispatchQueue.main.async if needed.

#Kotlin Event Dispatching

Dispatch events using NativeActionCoordinator.dispatchEvent:

Copied!
import android.os.Handler
import android.os.Looper
 
// Build your payload
val payload = JSONObject().apply {
put("result", processedData)
put("id", requestId)
}
 
// Must dispatch on main thread
Handler(Looper.getMainLooper()).post {
NativeActionCoordinator.dispatchEvent(
activity,
"Vendor\\MyPlugin\\Events\\ProcessingComplete",
payload.toString()
)
}

#Official Plugins & Dev Kit

Skip the complexity — browse ready-made plugins or get the Dev Kit to build your own. Visit the NativePHP Plugin Marketplace →