WebSockets: a server-side handle to a room, to broadcast to or close its connections from other server code

Open
#689 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript

Research direction

Start by reading the defineRoom API for *.ws.ts routes and the existing onRequest approach described in the issue; inspect how the generated binding in void.lock.json identifies a room instance. The proposal calls for a supported server-side handle usable from route handlers, auth hooks, queues, and cron jobs. Done means external code can address the same room instance as a client's connect() and use it to broadcast or close selected connections.

Written by the indexing model from the issue text.

Description

Void 1.5.0, Cloudflare target, defineRoom in a *.ws.ts route.

We need to reach a room from server code outside its own hooks. Our case is closing a user's open sockets when their Better Auth session is revoked: databaseHooks.session.delete.after fires on every revoke, sign-out and user deletion, but from there nothing in Void's API reaches the room's Durable Object. The docs only suggest re-checking records in onMessage and closing, which leaves an idle socket of a revoked session open until its next frame, and costs a database read per frame.

The only way we found is undocumented: the binding name Void generates (from void.lock.json), idFromName('<param>=<value>'), and a request handled in onRequest. We do not want to depend on those internals.

Proposal: a server-side handle to a room instance, for example:

import { getRoom } from 'void/ws';

const room = getRoom('/relay/:accountId', { params: { accountId } });
await room.broadcast(event);
await room.close({ code: 4001, reason: 'session revoked', filter: (connection) => connection.state.sessionId === id });

usable from route handlers, auth hooks, queues and cron jobs, and addressing the same instance a client's connect() would.

Dominant language
TypeScript
Stars
627
Forks
66
Avg merge
14h 38m
Merged PRs (30d)
179

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from voidzero-dev/void

All issues in voidzero-dev/void

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.