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

Resonate 1.x

Users

Signed-in user identity through pusher:signin, direct messages to a person on every device, and an online index.

webpatser/resonate-users v1.3.0. A Resonate connection has no identity of its own: the server only learns who a socket belongs to from the user_id in a presence channel's channel_data. This plugin implements the Pusher protocol's user authentication instead. A client sends pusher:signin with a signed blob, the server verifies it, and the socket carries an identity from then on.

client Resonate + resonate-users
| pusher:signin {auth, user_data} -> verify HMAC
| remember user 42
| join #server-to-user-42
| <- pusher:signin_success
...
your app: sendToUser(42, 'chat.dm', [...])
| <- chat.dm (every device user 42 has, on every node)

What it gives you

  • Direct messages. Address a person, not a room. Every device that person has signed in on receives it, on every node.
  • Notifications for rooms a user is not watching. They only have to be connected, not subscribed.
  • A working terminate_connections. Forced logout reaches users who never joined a presence channel.
  • An online index. Ask whether someone is connected, and on how many devices, without a metrics round-trip.

Install

composer require webpatser/resonate-users

Register the plugin in config/reverb.php:

'servers' => [
'reverb' => [
// ...
'plugins' => [
Webpatser\ResonateUsers\UserChannelPlugin::class,
],
],
],

Publish the config to change the Redis connection, the key prefix or the heartbeat:

php artisan vendor:publish --tag=resonate-users-config

Restart the server. Requirements: PHP 8.6+, Resonate 1.4+, and a Redis server reachable from every Resonate node.

The client half

Laravel ships the client half. Broadcast::userRoutes() registers /broadcasting/user-auth, and PusherBroadcaster::resolveAuthenticatedUser() signs the blob. Echo and pusher-js already speak the flow.

// routes/web.php or a service provider
Broadcast::userRoutes();
 
Broadcast::resolveAuthenticatedUserUsing(function ($request) {
return $request->user()
? ['id' => (string) $request->user()->id, 'user_info' => ['name' => $request->user()->name]]
: null;
});

Sending to a user

A user channel is an ordinary channel name, so anything that can broadcast can reach a person. The Pusher SDK helper works:

app('pusher')->sendToUser('42', 'chat.dm', ['body' => 'hello']);

To get the channel name for a plain broadcast:

use Webpatser\ResonateUsers\UserRegistry;
 
$channel = app(UserRegistry::class)->channelFor('42'); // #server-to-user-42

Asking who is online

use Webpatser\ResonateUsers\UserRegistry;
 
$users = app(UserRegistry::class);
 
$users->isOnline('42'); // bool
$users->deviceCount('42'); // 3
$users->sockets('42'); // ['1234.5678', ...] across every node
$users->snapshot(); // ['42' => 3, '7' => 1] for the whole application

On a server with several applications, pass the id: $users->isOnline('42', 'app-two').

An empty index means either nobody is connected or no socket server is reporting. Every running node refreshes a liveness key on each heartbeat, so you can tell the cases apart:

$users->hasLiveNode(); // bool: at least one node is reporting
$users->liveNodes(); // ['web-1-4242', ...] the nodes currently reporting

A Redis failure throws rather than answering false. A node that stops lets its key lapse after ttl seconds, since the plugin contract has no shutdown hook.

Events

Event When
UserSignedIn A connection proved an identity. Once per device, not once per person.
UserSignInRejected A signin was refused. Carries a reason constant.
UserWentOffline A user's last device disconnected anywhere in the cluster.

A client that drops and comes back produces an offline followed by a fresh sign-in. Hold the event for a grace period if you need a brief reconnect to pass unnoticed.

Security

  • A direct subscribe to a user channel is always refused. Membership is granted by signin and by nothing else.
  • The signature covers the socket id, so a payload lifted from one connection cannot be replayed onto another.
  • The key half of auth is checked against the connection's own application.
  • The first valid signin wins. A second one is refused.
  • A user id never reaches a Redis key raw: colons and glob metacharacters are percent-encoded.

Note: An application calling the Pusher SDK's sendToUser() against a bare Resonate broadcasts to a world-readable channel. Install this plugin before using it.

Configuration

Key Default Purpose
connection REDIS_* env Redis server holding the index. Per-plugin overrides use RESONATE_USERS_REDIS_*.
key_prefix users Namespace for every index key. Avoid colons. Env: RESONATE_USERS_KEY_PREFIX.
ttl 90 Seconds each node's key lives, refreshed on every heartbeat. Env: RESONATE_USERS_TTL.
heartbeat_interval 30 Seconds between reconcile ticks. Keep it well below ttl. Env: RESONATE_USERS_HEARTBEAT_INTERVAL.
user_channels true Whether a signed-in connection joins its own channel. Off records identity but stops delivery. Env: RESONATE_USERS_USER_CHANNELS.

Membership is written per node at {prefix}:{appId}:{userId}:{nodeId}, so a node that dies lets its share expire by TTL. The heartbeat tick rewrites each tracked user's set from the connections the node actually holds, so a lost removal self-heals.

Other plugins

If you also run Webhooks or Channel meter, use versions that ignore #-prefixed channels. Otherwise every sign-in posts a channel_occupied and, in the meter's case, opens a billable period for what is a user's session.