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

Resonate 1.x

Channel Meter

A Laravel-side webhook consumer that records channel occupancy periods as Eloquent models for billing and analytics.

webpatser/resonate-channel-meter v1.2.0. It records billable and observable channel occupancy periods from Webhooks events. It is a webhook consumer, not a Resonate plugin: it runs in your Laravel app.

Resonate process Laravel app
──────────────── ───────────
RedisRosterPlugin webhook
WebhookPlugin ─────────────► WebhookController + VerifyPusherSignature
│
▼
EventHandler
│
▼
channel_meter_periods table
│
▼
$chat->totalChannelMeterSeconds(...)

How it works

A channel_occupied event opens a channel_meter_periods row with started_at = time_ms and ended_at = null. The matching channel_vacated closes it. Both are idempotent: a redelivered webhook never duplicates a period or leaves an orphan close.

The table carries a polymorphic model_type and model_id. A resolver maps a channel name to the model it represents, using a {id} placeholder:

// config/resonate-channel-meter.php
'patterns' => [
'presence-chat.{id}' => \App\Models\Chat::class,
'presence-call.{id}' => \App\Models\Call::class,
],

presence-chat.42 resolves to (Chat::class, '42'). Only channels that match a pattern are metered. Any client can occupy a public channel of any name, so metering every channel would let an unauthenticated client create rows without bound. Prefer patterns on private- and presence- channels, which need authorization to join. To meter every channel anyway, set:

// config/resonate-channel-meter.php
'meter_unmatched_channels' => true, // or CHANNEL_METER_UNMATCHED_CHANNELS=true

Bind your own ChannelResolver::class in a service provider to replace the default ConfigChannelResolver when you need richer rules.

Install

composer require webpatser/resonate-channel-meter
php artisan migrate
php artisan vendor:publish --tag=resonate-channel-meter-config

The package migration is auto-loaded. Requirements: PHP 8.6+, Laravel 13, and webpatser/resonate-webhooks running against a Resonate server.

Register the webhook endpoint

The package does not register a route. Mount the controller behind the signature middleware:

use Webpatser\ResonateChannelMeter\Http\Controllers\WebhookController;
use Webpatser\ResonateChannelMeter\Http\Middleware\VerifyPusherSignature;
 
Route::post('/webhooks/resonate', WebhookController::class)
->middleware(VerifyPusherSignature::class);

Then point the webhooks plugin at that URL:

// config/resonate-webhooks.php
'endpoints' => [
[
'url' => env('APP_URL').'/webhooks/resonate',
'app_id' => '*',
'events' => ['channel_occupied', 'channel_vacated'],
],
],

The middleware verifies X-Pusher-Signature against the app secret in reverb.apps. An unsigned or forged request is rejected with 401, and an unknown X-Pusher-Key with 422. A missing or stale timestamp is also rejected with 422.

Query periods from a model

Apply the HasChannelMeter trait to any model the resolver maps to:

use Webpatser\ResonateChannelMeter\Concerns\HasChannelMeter;
 
class Chat extends Model
{
use HasChannelMeter;
}
$chat->channelMeterPeriods; // every period
$chat->openChannelMeterPeriods; // periods currently in progress
$chat->totalChannelMeterSeconds(); // sum of closed durations
$chat->totalChannelMeterSeconds($from, $to); // clamped to a window

Open periods are excluded from totals. Close stale periods explicitly if you need to bill an in-progress session.

Fully-occupied metering

By default a period spans plain room occupancy. Set min_members to 2 or more to meter only while the channel is fully occupied, for example billing a two-party reading only while both parties are present. Every membership event then re-evaluates the live member count, so you bind an AppScopedMembershipCounter under the MembershipCounter::class key. Its count() receives the application id and the channel name. The shipped NullMembershipCounter always returns 0. A host that uses Roster typically binds an adapter over it:

use Webpatser\ResonateChannelMeter\Contracts\AppScopedMembershipCounter;
use Webpatser\ResonateChannelMeter\Contracts\MembershipCounter;
use Webpatser\ResonateRoster\RoomRoster;
 
$this->app->singleton(MembershipCounter::class, fn () => new class implements AppScopedMembershipCounter {
public function count(string $appId, string $channel): int
{
return (new RoomRoster(config('resonate-roster')))->userCount($channel, $appId);
}
});

A counter written against the 1.x MembershipCounter contract, whose count(string $channel) gets only the channel name, still works under the same key. It cannot tell applications apart.

grace_seconds keeps the period open for a window after the count drops below the threshold, closing it at dropped-below + grace only if the count does not recover. A channel can go quiet after dropping, or the member_removed webhook can be lost, so schedule the backstop sweep:

// routes/console.php
Schedule::command('channel-meter:sweep')->everyTenSeconds();

Configuration

Key Default Purpose
patterns [] Channel-to-model patterns, each mapping a channel-name shape with a {id} placeholder to an Eloquent model class.
meter_unmatched_channels false Whether a channel matching no pattern is metered. Env: CHANNEL_METER_UNMATCHED_CHANNELS.
min_members 1 Members required to keep a period open. <= 1 is room occupancy, >= 2 is fully-occupied metering. Env: CHANNEL_METER_MIN_MEMBERS.
ignore_channel_prefixes ['#'] Channel prefixes never metered, applied before patterns. The protocol reserves # for server-owned channels such as the #server-to-user-{id} channels of Users.
grace_seconds 0 How long an open period survives below the threshold. Env: CHANNEL_METER_GRACE_SECONDS.
timestamp_window 300 Maximum age in seconds of a webhook's timestamp. Env: CHANNEL_METER_TIMESTAMP_WINDOW.

Caveats

  • Room mode records only channel_occupied and channel_vacated. Fully-occupied mode also consumes member_added and member_removed.
  • A unique index on (app_id, channel, open_slot) allows any number of closed periods but only one open one per channel.
  • Events older than the newest one applied are dropped. Each channel's high-water mark lives in the channel_meter_marks table.
  • started_at and ended_at come from the webhook's time_ms, not the receiver's clock, so a queued retry records the original moment.