Services

EventsServiceInterface

v1.0.0

In-process pub/sub event bus for cross-module notifications and one-off events.

Access

$ctx->events()?->emit('reports.refreshed', ['count' => 12]);

How To Use It

  • Use it for in-process coordination between Meteorack modules instead of hand-rolled global callbacks.
  • Keep payloads small and explicit so listeners can evolve independently and unsubscribe cleanly when needed.

Methods

public function emit(string $event, array $payload = []): void

Publishes an event with an optional payload.

public function on(string $event, callable $listener): callable

Registers a listener and returns an unsubscribe callable.

public function once(string $event, callable $listener): void

Registers a listener that runs only once.

Example

bootstrap.php
public function onSdkReady(SdkContext $ctx): void
{
    parent::onSdkReady($ctx);

    $ctx->events()?->on('billing.invoice_paid', [$this, 'refreshMetrics']);
}

public function refreshMetrics(array $payload): void
{
    $this->getSdkContext()->logger()->info('reports.metrics_refreshed', $payload);
}