Skip to content

Sessions: one App, a session per user - #66

Draft
ericgozzi wants to merge 12 commits into
mainfrom
feature/sessions
Draft

ericgozzi wants to merge 12 commits into
mainfrom
feature/sessions

Conversation

@ericgozzi

Copy link
Copy Markdown
Collaborator

Adds an option for one running App to serve several users, each with their own session: their own scene, workspaces and plugin data. Sessions are off by default (App(sessions=True) turns them on), so existing apps behave as before.

The design and its decisions are in src/compas_threejs/viewer/SESSIONS.md. Its first commit (92c9637) is the plan, written for review.

The model

App → Session → State → Workspaces

  • The App and its plugins are one instance, shared by everyone. Each client's data lives in its Session, not in copies of the plugins.
  • A State holds everything one client has: objects, buttons, actions and workspaces. A session is not a workspace: every session has all of the App's features, including several workspaces.
  • What app.py adds at startup is everyone's. That includes geometry, buttons and actions. A session sees it, and anything the session adds itself comes first.
  • Copy on edit: when a session edits a shared object, it gets its own copy (State.own), so one user moving a beam doesn't move it for everyone else.

What's in it

Phase What it adds
1 session.py: State, Session, SessionManager, current_session / current_state / current_workspace (contextvars), Session.bind. stores.py: MemorySessionStore, RedisSessionStore (session records only; the live State stays in the process)
2 ConnectionManager keyed by (session, workspace). Messages sent outside a session reach everyone and are replayed to new sessions. Plugin.send_everyone
3 App(sessions=True). SessionMiddleware: a cookie named compas_threejs_session for HTTP and WebSocket, with an authorize_session hook. Remote(session=...). Expired sessions are swept with AppServer.every
4–5 Copy on edit (State.own, State.shared). Plugin data per session: Plugin.state / session_state / new_session / end_session. Hiding and disabling follow the same rule as messages
6 The per_session descriptor: a plugin attribute kept per session, with .everyone(plugin) for everyone's value. Plugin data can start from everyone's, and can be set before the App exists

Redis is an optional extra (pip install compas_threejs[sessions]). docker compose up --build runs examples/sessions.py with Redis on http://127.0.0.1:9001.

Plugins

How each plugin keeps its data per session is in phase 6 of SESSIONS.md:

  • compas_threejs_plugins (Views, Essentials, IO, Layers, Draw, Weather) is adapted on its own feature/sessions branch.
  • compas_threejs_timber gives each user their own copy of the model on their first Timber action.

Open questions for review

  1. App shorthands called outside a session (for example in app.loop) when sessions=True: should they act on everyone, or raise an error? Proposed: act on everyone, by the same rule as messages.
  2. Session timeout default: 30 minutes is proposed. It is a setting (session_timeout=).
  3. Does this fit the FastAPI picture from the original request (endpoint, session store, connection manager)?

Testing

  • pytest: 118 passed, 5 skipped. The skipped tests need a real Redis: run docker compose up -d redis, then set COMPAS_THREEJS_REDIS_URL=redis://localhost:6379/0. Without it, the Redis tests use fakeredis.
  • tests/test_endpoint.py::test_the_server_sweeps_while_it_runs depends on timing. It failed once on a cold first run and passed on every rerun.
  • End to end with two browsers (Playwright): each browser gets its own session; everyone's content reaches both, and one session's edits stay in that session.

Before merging

  • Answer the open questions above
  • CHANGELOG entry
  • Merge the plugins' feature/sessions branches after this PR

🤖 Generated with Claude Code

ericgozzi and others added 12 commits November 9, 2026 08:54
Several clients on one App, each with its own Session and State, while app.py
and the plugins stay one shared instance. Three levels: endpoint, SessionStore,
ConnectionManager; six phases; what each plugin keeps for one client.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each client will have its own Session and State while app.py and the plugins
stay one instance. This phase adds the parts, with every client still in the
default session, so nothing changes in behaviour:

- State: a client's workspaces, registries (geometry, materials, metadata,
  buttons, actions) and plugin data. Inbox keeps its registries' old names, as
  views onto the current session's State; each Workspace registers into its own.
- Session and SessionManager: the live sessions; a default one that never ends.
- SessionStore: the records of sessions - MemorySessionStore, and
  RedisSessionStore (extra "sessions"), Redis in docker-compose.yml.
- current_session/current_state/current_workspace, bound for every message in
  App.on_message (not only plugin messages); Session.bind for threads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
What is registered outside of any session (at startup) is everyone's; what a
session registers is its own and comes first. Kept once, in the default
session's State; Inbox.buttons and Inbox.action_registry look in the session's,
then in those. A shared callback runs with the clicking session bound.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ConnectionManager (connections.py) takes the sockets and the persisted
messages out of AppServer, keyed by session and workspace, at three reaches:
one workspace, every workspace of a session, everyone. The default session's
messages are everyone's, so what app.py adds at startup reaches and is
replayed to every session.

Every send carries its session: a Workspace its own, other code the bound one,
else the default. Plugin.send_all follows the reach rule (inside a session:
that session; outside: everyone); Plugin.send_everyone is new. Sessions count
their open tabs; an ended session's persisted messages are dropped.
AppServer.resolve_session picks a connection's session, set in phase 3.

Older code keeps working: on_message(data, workspace_id) overrides, a replaced
Plugin._send, outboxes without session_id.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
App(sessions=True): SessionMiddleware (endpoint.py), one ASGI middleware for
HTTP and the WebSocket, finds each request's session from a cookie - random
id, HttpOnly, SameSite=Lax, Secure over HTTPS - starts one if there's none or
it expired, asks App.authorize_session callbacks (403 when refused), and binds
it: plugin routes, sync or async, see current_session(). Remote(session=...)
joins one with the cookie. The store is touched at most every timeout/10;
AppServer.every sweeps expired sessions while serving.

examples/sessions.py and docker/Dockerfile: the example with Redis in Docker
(docker compose up --build), checked with two real browsers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er session

Phase 4: each session's State reads through to everyone's (the default
session's, holding what app.py added at startup) and copies what it changes
(State.own): a gizmo move, a color edit, an object action or
Workspace.transform_geometry from a session changes that session's own copy,
with the same guid, so only its tabs swap it. Removing everyone's object from
a session is kept for that session's tabs. Without sessions, objects are
changed in place, as before.

Phase 5: Plugin.session_state() / Plugin.state, per session;
Plugin.new_session / end_session; hide/show/disable/enable for the current
session inside one, for everyone outside. Plugin handlers are found on the
class, so properties aren't run.

examples/sessions.py gets "Move the box", checked in two browsers in Docker.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e the App

Plugin.inherit_state(everyone) makes a new session's data from the default
session's (by default a fresh session_state()); Plugin.everyone is that data.
Plugin.state also works before the plugin is added to an App: what is set
then becomes everyone's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
State.own_material no longer copies the geometry too: restyling everyone's
object (ghosting a layer) leaves its geometry everyone's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Declared on the plugin class (_history = per_session(None),
_unsnappable = per_session(set, inherit=set)); read and set like any
attribute, the current session's value. For plugins with many attributes
per client, without writing a property for each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant