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

Resonate 1.x

Webhooks

Pusher-style signed HTTP webhooks for channel occupancy, presence members and client events, claimed once per cluster.

webpatser/resonate-webhooks v1.2.0. It POSTs channel_occupied, channel_vacated, member_added, member_removed and client_event to your backend, so your application reacts to channel activity without polling the socket server.

Resonate fires no webhooks on its own. Firing them from per-node channel events would double-send channel_occupied when a channel has subscribers on two nodes, and send member_removed for a user who is still connected on another node. The plugin reads occupancy from Roster, which keeps a cluster-wide membership count, and claims each edge once per cluster with an atomic Redis flag (SET NX). member_added is delivered at least once.

How it works

  • On a subscribe, the plugin reads the cluster connection count from the roster for that connection's application. The node that wins the flag emits channel_occupied.
  • On an unsubscribe or close that leaves the count at zero, one node wins the flag delete and emits channel_vacated.
  • member_added and member_removed use the same claim, keyed per user.
  • A reconcile tick re-checks every tracked channel against the roster, so an edge missed during a crash is recovered.
  • Connection hooks only buffer events and never make HTTP calls. A delivery tick coalesces the buffer into one POST per endpoint and sends each in a detached fiber. A failed delivery is retried with exponential backoff and dropped after max_attempts.

Treat member_added as idempotent: after a flag lapse your backend may receive a duplicate for a user who is still present, but never loses one. Delivery is in-process, so undelivered webhooks are lost if the server process crashes.

Install

composer require webpatser/resonate-webhooks

This pulls in webpatser/resonate-roster. Publish both configs if you want to change defaults:

php artisan vendor:publish --tag=resonate-roster-config
php artisan vendor:publish --tag=resonate-webhooks-config

Requirements: PHP 8.6+, Resonate 1.4+, webpatser/resonate-roster 1.0+ configured with track => all, and a Redis server reachable from the Resonate process.

Set up

Let the roster track all channels, not just presence channels:

RESONATE_ROSTER_TRACK=all

Register both plugins in config/reverb.php. The roster must be listed before the webhooks plugin, because plugin hooks run in array order and the roster's Redis writes must land before this plugin reads the cluster count.

'servers' => [
'reverb' => [
// ...
'plugins' => [
\Webpatser\ResonateRoster\RedisRosterPlugin::class, // first
\Webpatser\ResonateWebhooks\WebhookPlugin::class, // second
],
],
],

Declare your endpoints in config/resonate-webhooks.php:

'endpoints' => [
[
'url' => env('RESONATE_WEBHOOK_URL'),
'app_id' => '*', // or a specific application id
'events' => [
'channel_occupied', 'channel_vacated',
'member_added', 'member_removed', 'client_event',
],
],
],

Restart Resonate with php artisan resonate:start, or resonate:reload for a zero-downtime swap.

The webhook request

Each delivery is a Pusher-format POST, signed so the receiver can verify it:

POST {endpoint.url}
Content-Type: application/json
X-Pusher-Key: {app key}
X-Pusher-Signature: hmac_sha256(rawBody, app secret)
 
{"time_ms": 1700000000000, "events": [
{"name": "channel_occupied", "channel": "presence-chat.42"},
{"name": "member_added", "channel": "presence-chat.42", "user_id": "7"},
{"name": "client_event", "channel": "presence-chat.42", "event": "client-typing",
"data": "{...}", "socket_id": "...", "user_id": "7"}
]}

The key and secret come from the matching app in reverb.apps, so a Pusher webhook consumer verifies it unchanged:

$expected = hash_hmac('sha256', $request->getContent(), config('reverb.apps.apps.0.secret'));
 
abort_unless(hash_equals($expected, $request->header('X-Pusher-Signature')), 403);

To store occupancy periods from these webhooks, see Channel meter.

Configuration

Key Default Purpose
connection REDIS_* env Redis server. Must be the same server and database as resonate-roster.
key_prefix wh Namespace for the edge-detection flag keys, {prefix}:{kind}:{appId}:{channel}. Env: RESONATE_WEBHOOKS_PREFIX.
ttl 90 Flag-key TTL in seconds. A value below twice reconcile_interval is raised to twice reconcile_interval. Env: RESONATE_WEBHOOKS_TTL.
flush_interval 1.0 Seconds between delivery ticks. Events in that window are coalesced. Env: RESONATE_WEBHOOKS_FLUSH.
reconcile_interval 30.0 Seconds between occupancy reconcile ticks. Env: RESONATE_WEBHOOKS_RECONCILE.
ignore_channel_prefixes ['#'] Channel prefixes never reported on. The protocol reserves # for channels the server owns, such as the #server-to-user-{id} channels of Users.
max_attempts 5 Delivery attempts before a webhook is dropped. Env: RESONATE_WEBHOOKS_MAX_ATTEMPTS.
endpoints [] The endpoints that receive webhooks.

Limits

Four bounds keep a slow or unreachable endpoint from growing memory without limit. Set any to 0 to restore unbounded behaviour.

Key Env variable Default Meaning
max_buffered_events RESONATE_WEBHOOKS_MAX_BUFFERED_EVENTS 10000 Events held between delivery ticks.
max_pending_deliveries RESONATE_WEBHOOKS_MAX_PENDING 1000 Built deliveries awaiting or retrying.
max_request_bytes RESONATE_WEBHOOKS_MAX_REQUEST_BYTES 1048576 Largest encoded size of the events in one request. A bigger batch is split into several signed POSTs, sent in order.
max_in_flight_per_endpoint RESONATE_WEBHOOKS_MAX_IN_FLIGHT 16 Concurrent POSTs to one endpoint URL.

When the event buffer is full, room is made in this order: a buffered occupied/vacated or added/removed pair is cancelled, then the oldest client_event is dropped, then the oldest event. When an occupancy or membership edge is dropped, a warning is logged saying receivers need a resync. Redirects are not followed: a 3xx response counts as a failed delivery.

Caveats

  • The plugin must point at the same Redis server and database as the roster.
  • With the roster in its default presence mode, channel_occupied and channel_vacated do not fire for public or private channels.
  • Occupancy is read and claimed per application.
  • Not Redis Cluster safe: occupancy is read with SCAN, which on Redis Cluster walks only one node. Use a single Redis server.
  • This version requires roster 1.0. Coming from a roster older than 0.3.0, upgrade both packages through 0.7.x first.