~/docs
Resonate
Resonate
1.x 1.5.0, current
Resonate
Toggle sidebar
Resonate
webpatser/resonate 1.5.0

Resonate 1.x

Server Plugins

The plugin contracts, the PluginContext API and per-connection state for running application logic inside the Resonate process.

Resonate is a product-agnostic Pusher relay. A plugin runs your own stateful logic inside the Resonate process: periodic timers, custom message types and connection bookkeeping, without a second service. Because the server runs on a fiber runtime, a plugin can do async DB or Redis work from a timer and the loop keeps serving other connections while it waits.

If all you need is to broadcast from your app to clients, you do not need a plugin. Use ordinary Laravel broadcasting.

You want to Capability
Handle a custom client event type yourself MessageInterceptor
Track connections in your own registry ConnectionLifecycle
Push something to clients on a schedule TickScheduler

First-party plugins

Each is an opt-in webpatser/* package with its own page.

Package Page Where it runs
webpatser/resonate-webhooks Webhooks Server plugin
webpatser/resonate-users Users Server plugin
webpatser/resonate-user-cap User cap Server plugin
webpatser/resonate-roster Roster Server plugin
webpatser/resonate-delivery Delivery Server plugin and broadcaster
webpatser/resonate-token-auth Token auth Server plugin
webpatser/resonate-upstream Upstream Server plugin
webpatser/resonate-pulse Pulse Laravel Pulse recorders and cards
webpatser/resonate-channel-meter Channel meter Laravel app, webhook consumer

Combinations that work well together:

  • roster, webhooks and channel-meter: push channel activity to the Laravel app and bill or audit channel sessions.
  • roster, webhooks and pulse: see the whole cluster on the Pulse dashboard.
  • token-auth and user-cap: let mobile clients authenticate without cookies and cap their device fan-out.
  • users and roster: who is online as a person, and who is in which room.
  • delivery with any other plugin: reconnect replay is independent.

Versions

The first-party plugins (roster, webhooks, user-cap, token-auth, delivery, users, pulse, channel-meter) are versioned with Resonate at 1.x. The current plugin releases require webpatser/resonate ^1.4 (channel-meter requires no Resonate package because it runs in the Laravel app). Resonate 1.x follows semantic versioning, so the plugin contracts do not break within 1.x. Patch numbers move independently. Plugins released before 1.0 (0.7.x, requiring ^0.7) need the 1.x release of the same plugin when you upgrade Resonate. webpatser/resonate-upstream is at 0.1.

The contracts

Every plugin implements the ServerPlugin marker contract and any mix of three capability interfaces. They live in Webpatser\Resonate\Plugins\Contracts.

interface ServerPlugin
{
// Booted once at server start, on the loop, before connections are accepted.
public function boot(PluginContext $context): void;
}
 
interface MessageInterceptor
{
// Runs before the standard pusher: / client-* routing.
public function onMessage(Connection $from, array $event): MessageDisposition;
}
 
interface ConnectionLifecycle
{
public function onOpen(Connection $connection): void;
public function onClose(Connection $connection): void;
public function onSubscribe(Connection $connection, Channel $channel): void;
public function onUnsubscribe(Connection $connection, Channel $channel): void;
}
 
interface TickScheduler
{
// [['interval' => float seconds, 'callback' => callable(): void], ...]
public function ticks(): array;
}

A plugin only implements the interfaces it needs. A timer-only plugin implements ServerPlugin and TickScheduler. The examples page has a complete plugin.

Message dispositions

onMessage() returns a MessageDisposition:

  • Relay: not your event. Fall through to the normal pusher: and client-* routing. Return this for anything you do not own so ordinary Pusher traffic is never disturbed.
  • Handled: you consumed the message. Skip core routing.
  • Rejected: you refused the message. Skip core routing. You are responsible for having sent your own error frame to the connection.

When every plugin returns Relay, routing is byte-identical to a server with no plugins at all.

Registration

Add the plugin class to the plugins array of your server in config/reverb.php:

'servers' => [
'reverb' => [
// ...
'plugins' => [
App\Resonate\ChatPlugin::class,
],
],
],

Each entry is a class name. Plugins are:

  • resolved through the container, so constructor dependencies inject;
  • booted once at server start, on the loop, before the first connection;
  • loaded in array order, which is also the onMessage() interceptor order.

Restart the server (php artisan resonate:start, or resonate:reload for a zero-downtime swap) to pick up plugin changes.

PluginContext

boot() receives a PluginContext, the supported way to reach into the server.

Method Purpose
sendTo(Connection $c, string $event, array $data = []) Push one event to one connection.
broadcast($app, string $channel, string $event, array $data = [], ?Connection $except = null) Send to every connection on a channel. Routed through the event dispatcher, so with scaling enabled it reaches every node.
terminate(Connection $c, ?string $event = null, array $data = []) Optionally send a final event, then close the connection (local node only).
unsubscribe(Connection $c, string $channel) Remove a connection from one channel, leaving the socket and its other subscriptions intact.
joinUserChannel(Connection $c, string $userId) Subscribe a connection to #server-to-user-{id} once the plugin has verified its identity. Applies the subscription cap and plugin onSubscribe hooks; throws when refused.
connectionsOn($app, string $channel): array The live ChannelConnections for a channel on this node.
application(?string $id = null): Application Resolve one application.
applications(): Collection Every configured application.

Resolving an application

broadcast() and connectionsOn() take $app as an Application, an app id string or null:

  • inside onMessage, onOpen, onClose, onSubscribe and onUnsubscribe you have a Connection, so pass $connection->app();
  • inside a ticks() callback there is no connection, so pass an app id string, or null to use the sole configured app.

On a server with more than one app configured, null is ambiguous: application(null) throws InvalidApplication. Pass the id explicitly.

Per-connection state

Connection carries a plugin-owned state bag. It is per socket and lives for the whole connection, unlike presence channel_data, which is per channel and is lost on unsubscribe.

$connection->setState('hb.connected_at', now()); // store
$connection->state('hb.connected_at'); // read (null if absent)
$connection->state('hb.role', 'guest'); // read with a default
$connection->state(); // the whole bag
$connection->hasState('hb.connected_at'); // bool
$connection->forgetState('hb.connected_at'); // remove

Namespace your keys (hb.*, chat.*) so two plugins never collide.

Exception isolation

Every plugin call is wrapped in a try/catch: boot(), onMessage(), the lifecycle hooks, ticks() and each scheduled tick body. A plugin that throws is logged and skipped. It cannot break the core connection lifecycle, the message loop or another plugin. A throwing onMessage() is treated as Relay.

Notes and caveats

  • Ticks are serialised for you. The scheduler does not start a tick while the previous run of the same registration is still pending. It skips that beat and logs it. A callback that outruns its interval needs no "running" flag of its own. Serialisation is per registration, so one plugin's slow tick never holds up another's.
  • onUnsubscribe versus onClose. onUnsubscribe fires for the explicit pusher:unsubscribe event, a connection leaving one channel while staying open. A connection that closes is reported once through onClose, not as one onUnsubscribe per channel. PluginContext::unsubscribe() is a direct server-side action and does not itself emit onUnsubscribe.
  • Reading the presence user_id. For a presence channel the subscribing user's id lives in the ChannelConnection: $channel->connections()[$connection->id()]?->data('user_id').
  • Client address. $connection->remoteAddress() is the TCP peer, which behind a reverse proxy is the proxy for every connection. $connection->clientAddress() is the address the core rate limiter keys on: the TCP peer, or, when that peer is listed in servers.reverb.trusted_proxies, the right-most X-Forwarded-For entry that is not itself a trusted proxy. Key per-client limits on clientAddress().
  • terminate() is local. It only ends connections on the node that runs it. Cross-node termination needs a pub/sub envelope.
  • Async work belongs in fibers. Tick callbacks already run inside a fiber, so suspending calls are fine. Do not call blocking I/O.
  • Keep plugins product-specific in your app. Resonate ships the contracts. The plugin classes live in your application, not in the Resonate package.