PHPackages                             webpatser/resonate-user-cap - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. webpatser/resonate-user-cap

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

webpatser/resonate-user-cap
===========================

Per-user connection cap for Resonate: cluster-wide limit on tabs and devices per user

v0.2.0(2mo ago)001MITPHPPHP ^8.5CI passing

Since May 25Pushed 3w agoCompare

[ Source](https://github.com/webpatser/resonate-user-cap)[ Packagist](https://packagist.org/packages/webpatser/resonate-user-cap)[ RSS](/packages/webpatser-resonate-user-cap/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (7)Versions (3)Used By (1)

Resonate User Cap
=================

[](#resonate-user-cap)

A [Resonate](https://github.com/webpatser/resonate) plugin that caps the cluster-wide connection count per user, so one user cannot open hundreds of tabs or run a runaway client against your socket server.

Resonate ships an app-level `max_connections`, which caps the total. This caps per *user*.

How it works
------------

[](#how-it-works)

### Identity from the first presence subscription

[](#identity-from-the-first-presence-subscription)

A WebSocket connection is anonymous until something tells the server who it belongs to. Pusher presence channels carry the user identity in their `channel_data`, so this plugin treats the first presence subscription as the moment a connection takes on a `user_id`. From then on, the connection counts against that user.

Connections that never subscribe to a presence channel are never counted and never capped.

### Cluster-wide count, self-healing

[](#cluster-wide-count-self-healing)

For each `(app_id, user_id)`, the plugin keeps a per-node Redis set of the socket ids it currently holds:

```
{prefix}:{app}:{user}:{node}    SET    (TTL refreshed by heartbeat)

```

A user's cluster-wide count is the union of every node's set: `SCAN {prefix}:{app}:{user}:*` then `SCARD` each. This is the same self-healing pattern as `webpatser/resonate-roster`: a dead node's set expires on its own, and a live node never holds a dead node's count open.

### Terminate on cap

[](#terminate-on-cap)

On a presence subscribe the plugin reads the current cluster count. If accepting the connection would meet or exceed the cap, the plugin sends a Pusher error frame and closes the connection:

```
{"event": "pusher:error", "data": {"code": 4301, "message": "Too many connections for this user"}}
```

Otherwise it adds the socket to this node's set and remembers the identity in connection state, so `onClose` can decrement cleanly when the connection drops.

A check-then-add against the union can race two nodes into a one-over overshoot under heavy concurrent connect bursts; the next check immediately corrects it. An under-cap is not possible.

Installation
------------

[](#installation)

```
composer require webpatser/resonate-user-cap
```

Publish the config to change defaults:

```
php artisan vendor:publish --tag=resonate-user-cap-config
```

Registering the plugin
----------------------

[](#registering-the-plugin)

```
// config/reverb.php
'servers' => [
    'reverb' => [
        // ...
        'plugins' => [
            \Webpatser\ResonateUserCap\PresenceCapPlugin::class,
        ],
    ],
],
```

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

Configuration
-------------

[](#configuration)

```
// config/resonate-user-cap.php
return [
    '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',
];
```

KeyDefaultPurpose`connection``REDIS_*` envRedis server; every node must point at the same server and database.`key_prefix``cap`Namespace for the per-user sets.`ttl``90`Seconds each node's set lives; refreshed on every heartbeat.`heartbeat_interval``30.0`Seconds between heartbeat ticks.`default``5`Default cluster-wide cap per user. `0` disables capping.`per_app``[]`Per-app overrides keyed by app id.`error_code` / `error_message``4301` / textThe `pusher:error` payload sent before close.Notes and caveats
-----------------

[](#notes-and-caveats)

- **Presence is the identity source.** A connection that subscribes only to public or private channels has no `user_id`, so this plugin cannot and does not cap it. Pair with a custom auth plugin if you need to cap unauthenticated connections too.
- **Reject-new, not kick-oldest.** When a user is at the cap, the *new* connection is terminated. The existing ones are untouched.
- **Eventually consistent.** Concurrent connect bursts from one user across nodes may temporarily overshoot the cap by one; the next subscribe corrects it.
- **One identity per connection.** A connection's `user_id` is taken from its *first* presence subscription. Later presence subscriptions with a different `user_id` are ignored for capping.

Requirements
------------

[](#requirements)

- PHP 8.5+
- Resonate 0.4+
- A Redis server reachable from every Resonate node

Testing
-------

[](#testing)

```
composer test
```

Tests that touch Redis expect a server on `127.0.0.1:6379` and use database 15; they skip cleanly when no Redis is reachable.

License
-------

[](#license)

MIT. See [LICENSE](LICENSE).

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance92

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

60d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e442a1d15a5b64438f3b471acfded80951afb1bed23641cfd80c5254099eab9d?d=identicon)[webpatser](/maintainers/webpatser)

---

Top Contributors

[![webpatser](https://avatars.githubusercontent.com/u/25720?v=4)](https://github.com/webpatser "webpatser (6 commits)")

---

Tags

WebSocketsrate limitBroadcastingpresencecapresonate

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/webpatser-resonate-user-cap/health.svg)

```
[![Health](https://phpackages.com/badges/webpatser-resonate-user-cap/health.svg)](https://phpackages.com/packages/webpatser-resonate-user-cap)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.3M347](/packages/psalm-plugin-laravel)[illuminate/events

The Illuminate Events package.

13557.0M2.2k](/packages/illuminate-events)[illuminate/pipeline

The Illuminate Pipeline package.

9349.2M291](/packages/illuminate-pipeline)[illuminate/session

The Illuminate Session package.

9939.3M861](/packages/illuminate-session)[illuminate/pagination

The Illuminate Pagination package.

10534.1M1.1k](/packages/illuminate-pagination)[illuminate/broadcasting

The Illuminate Broadcasting package.

7127.2M221](/packages/illuminate-broadcasting)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
