webpatser/resonate-user-cap v1.1.0. Resonate's app-level max_connections caps the total. This plugin caps per user, so one user cannot open hundreds of tabs or run a runaway client against your socket server.
How it works
A connection is anonymous until something tells the server who it belongs to. The plugin takes the user_id from the first presence subscribe whose signature it can verify. From then on the connection counts against that user. Connections that never subscribe to a presence channel are never counted and never capped.
For each app and user the plugin keeps a per-node Redis set of socket ids, plus a registry set of nodes. The cluster-wide count is the sum of the registered nodes' sets. A dead node's set expires on its own, so a live node never holds a dead node's count open.
The cap is applied to the inbound pusher:subscribe message, before Resonate establishes the subscription. If accepting the connection would meet or exceed the cap, the plugin sends a Pusher error frame, closes the connection and consumes the message:
{"event": "pusher:error", "data": {"code": 4301, "message": "Too many connections for this user"}}
The client never joins the channel and never sees the presence member list. The plugin verifies the presence signature itself before it trusts the user_id, so nobody can burn another user's slots by claiming their identity. A heartbeat rebuilds each tracked user's set from the connections the node holds, so a lost decrement costs a slot for at most one beat.
Install
composer require webpatser/resonate-user-capphp artisan vendor:publish --tag=resonate-user-cap-config
Register the plugin in config/reverb.php:
'servers' => [ 'reverb' => [ // ... 'plugins' => [ \Webpatser\ResonateUserCap\PresenceCapPlugin::class, ], ],],
Restart Resonate. Requirements: PHP 8.6+, Resonate 1.4+, and a Redis server (or a Redis Cluster) reachable from every Resonate node.
Configuration
// config/resonate-user-cap.phpreturn [ 'connection' => [ /* Redis */ ], 'key_prefix' => 'cap', 'ttl' => 90, 'heartbeat_interval' => 30.0, 'default' => 5, // 0 disables capping 'per_app' => [ // 'app-id' => 3, ], 'error_code' => 4301, 'error_message' => 'Too many connections for this user', 'fail_open' => false,];
| Key | Default | Purpose |
|---|---|---|
connection |
REDIS_* env |
Redis server. Every node must point at the same server and database. Per-plugin overrides use RESONATE_USER_CAP_REDIS_*. |
connection.cluster |
RESONATE_USER_CAP_REDIS_CLUSTER_NODES |
Comma-separated Redis Cluster seeds (host:port or URIs). Empty means a single server. |
key_prefix |
cap |
Namespace for the per-user sets. Must not contain { or }. Env: RESONATE_USER_CAP_PREFIX. |
ttl |
90 |
Seconds each node's set lives, refreshed on every heartbeat. Env: RESONATE_USER_CAP_TTL. |
heartbeat_interval |
30.0 |
Seconds between heartbeat ticks. Env: RESONATE_USER_CAP_HEARTBEAT. |
default |
5 |
Default cluster-wide cap per user. 0 disables capping. Env: RESONATE_USER_CAP_DEFAULT. |
per_app |
[] |
Per-app overrides keyed by app id. |
error_code / error_message |
4301 / text |
The pusher:error payload sent before close. Env: RESONATE_USER_CAP_ERROR_CODE, RESONATE_USER_CAP_ERROR_MESSAGE. |
fail_open |
false |
When Redis cannot answer, false refuses the presence subscribe with the error payload, true admits it uncounted. Env: RESONATE_USER_CAP_FAIL_OPEN. |
Redis Cluster
Point the plugin at a Redis Cluster by listing seed nodes:
RESONATE_USER_CAP_REDIS_CLUSTER_NODES=10.0.0.1:7000,10.0.0.2:7000,10.0.0.3:7000RESONATE_USER_CAP_REDIS_PASSWORD=secret
The client discovers the other masters from the seeds and routes each command to the master that owns its slot. Seeds take username, password and TLS (scheme tls or rediss) from the rest of the connection config, or from its url. A seed may also be a full URI such as rediss://user:[email protected]:7000. Cluster always uses database 0. Leave the variable empty for a single Redis server.
Caveats
- Presence is the identity source. A connection that subscribes only to public or private channels has no
user_idand is not capped. - Reject-new, not kick-oldest. The new connection is terminated. Existing ones are untouched.
- Eventually consistent. Concurrent connect bursts from one user across nodes may overshoot the cap by one. The next subscribe corrects it.
- One identity per connection. The
user_idcomes from the first verified presence subscribe. Later ones with a different id are ignored for capping. - Counted one step early. If Resonate then refuses the subscribe for its own reasons, the socket stays counted until it closes.
- Fails closed. If Redis cannot be reached, a new presence subscribe is refused. Set
fail_opento prefer availability.
Upgrading from 0.7? See the UPGRADE.md in the plugin repository.