feat(examples): realtime session queue reference (server gate + web client)#187
Merged
Conversation
commit: |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 83ac1d7. Configure here.
…slow-mint guard, client race guards
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 7b00715. Configure here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Customers with a realtime concurrency limit (say, 10 sessions) need a product answer for the 11th user: wait in a fair line with live position feedback, instead of a failed connection. This PR adds
examples/realtime-session-queue— a runnable reference showing how to build that gate on your own backend, in front ofclient.realtime.connect(). The scenario modeled is a virtual try-on experience (lucy-vton-latest), but the pattern applies to any capacity-limited realtime model.The idea
Your backend already holds the API key and mints ephemeral client tokens for your app. A session queue is nothing more than deciding when to mint the next token:
Three properties make it robust without being clever:
expiresInmatches the queue's 45s claim grace (the connect window); a granted user who never connects loses nothing for anyone else.constraints: { realtime: { maxSessionDuration } }, so a backgrounded or killed app can never squat a slot and the line always moves.Every join takes its own spot: capacity limits concurrent sessions, not users.
Try it
Open two tabs and hit Try it on in each (a sample garment is preloaded). With
QUEUE_CAPACITY=1, the second tab waits with a live position and inherits the slot the moment the first session ends.What's inside
server/queue.ts— the whole algorithm in one file: FIFO line + capacity-bounded leases, with time and token-minting injected so the tests are instant and deterministic (12 tests).server/main.ts— the five-route HTTP protocol (join / poll-as-claim / started / release / stats).web/— a React client whoseuseQueuehook uses onlyfetch, timers, and React state.Verified live end-to-end against a limit-3 test account: real try-on sessions, queue position → slot handover between users, and automatic recovery through the over-limit backstop.
Note
Low Risk
Changes are confined to examples and lockfile; no SDK or production runtime behavior is modified.
Overview
Adds
examples/realtime-session-queue, a runnable pattern for fair waiting lines when realtime concurrency is capped: users join with a ticket, poll for position, and only then receive a short-lived ephemeral token to callrealtime.connect()(virtual try-on onlucy-vton-latest).The Express gatekeeper wires
Queuetotokens.create()with a 45s connect window andmaxSessionDurationon the token, plus HTTP routes for join, poll-as-claim,started,release(ended/limit_reachedrequeue-at-head), and stats.server/queue.tsholds FIFO waiting, capacity leases, no-show reclaim, and mint races; 12 Node tests cover the semantics with injected time/mint.The Vite/React client uses a
useQueuehook (fetch + polling) andTryOnSessionfor camera + realtime, including 1013 / session-limit handling that reportslimit_reachedback to the server. A long README documents protocol, slot lifecycle, and production hardening.Elsewhere the diff is cosmetic: import order / formatting in a few examples, trailing newline cleanup, and
pnpm-lock.yamlentries for the new workspace example (plus unrelated lockfile churn).Reviewed by Cursor Bugbot for commit 1e20fe5. Bugbot is set up for automated code reviews on this repo. Configure here.