Skip to content

relay ​

@c9up/relay: realtime messages to the browser: channels broadcast over Server-Sent Events, and hubs a stock SignalR client speaks to over WebSockets or SSE. Guides: Broadcasting, Hubs.

Installation ​

bash
pnpm add @c9up/relay
ream configure @c9up/relay

ream configure @c9up/relay registers the provider and writes config/relay.ts. A Redis bus by connection name needs @c9up/quasar, an optional peer dependency. The subscribe routes read a JSON body: keep ream's body parser in the router middleware.

Entry points ​

ImportContents
@c9up/relayRelay, Hub, SignalRAdapter, RedisRelayTransport, transports, defineConfig, registerHubRoutes and the types. Also re-exports RelayProvider.
@c9up/relay/providerRelayProvider (default export).
@c9up/relay/configureconfigure(codemods), run by ream configure @c9up/relay.
@c9up/relay/services/mainThe application's Relay (default export), plus setRelay, getRelay, clearRelay. Before the provider binds one, it builds a default Relay; what was registered on it moves to the provider's.
@c9up/relay/testingFakeRelay, which records broadcasts for assertions.

Exports ​

@c9up/relay ​

ExportKindDescription
RelayclassChannels, authorizers, broadcasts, hub mounting.
HubclassAbstract base of a hub: methods named on<Event> handle the client's invocations.
SignalRAdapterclassThe SignalR JSON protocol for one hub: negotiate, handshake, frames.
RedisRelayTransportclassRedis pub/sub as the bus between instances.
transportsconsttransports.redis(options), the bus factory a config names.
defineConfigfunctiondefineConfig<T extends RelayConfig>(config: T): T. Returns the config unchanged.
registerHubRoutesfunctionMounts one hub's routes on a router; what relay.hub() calls through the provider.
splitFramesfunctionsplitFrames(body: string): string[]: splits a POST body on the SignalR record separator.
PING_CHANNELconst'$$relay/ping', the event name of keep-alive frames.
RelayProviderproviderSame as @c9up/relay/provider.
errorsnamespaceThe package's errors, one class per code, see Errors
RelayConfiginterfaceSee Configuration.
RelayContextinterfaceWhat an authorizer receives: auth?, session?, request?, bouncer?.
RelayAuthinterface{ isAuthenticated: boolean; user?: { id: string; [key: string]: unknown }; roles?: string[]; permissions?: string[] }.
ChannelAuthorizertype(ctx: RelayContext, params: TParams) => Promise<boolean> | boolean.
RelayTransportinterfaceA bus: publish(channel, message), subscribe(channel, handler), optional unsubscribe(channel, handler?) and disconnect().
RelayTransportFactorytype() => RelayTransport.
RelayPubSubClientinterfaceThe pub/sub commands RedisRelayTransport issues: publish, subscribe, unsubscribe, optional quit.
RelayPubSubResolvertypeRelayPubSubClient | (() => RelayPubSubClient | Promise<RelayPubSubClient>).
RelayLifecycleEventsinterfacePayloads of connect, disconnect, subscribe, unsubscribe, broadcast.
RelayRouteBuilderinterfaceThe route object a customizer receives: middleware, use, guard, role, permission.
RelayRouteCustomizertype(route: RelayRouteBuilder) => void.
RelaySseStreaminterfaceAn SSE stream as the relay writes to it: id, isOpen(), send(event, data, eventId?), onClose(cb), end().
SubscribeResulttypeSubscribeSuccess | SubscribeFailure.
SubscribeSuccessinterface{ ok: true }.
SubscribeFailureinterface{ ok: false; status: number; code: string }.
HubContextinterfaceWhat a hub handler receives.
HubAuthinterfaceThe request's auth as a hub reads it: { isAuthenticated: boolean; strategy?: string; user?: Record<string, unknown>; roles?: string[]; permissions?: string[]; hasAllPermissions?(permissions: readonly string[]): Promise<boolean> }.
HubDenialinterfaceA refusal by a hub's guards: { status: 401 | 403; code: 'E_RELAY_UNAUTHORIZED' | 'E_RELAY_FORBIDDEN'; message: string }.
HubGuardOptionsinterface{ guard?: string; guards?: string[]; roles?: string[]; permissions?: string[] }.
ConnectedHubClientinterfaceA connection as a transport registers it: { id: string; groups: Set<string>; auth: HubAuth; send(event: string, data: unknown): void; close?(): void }. close ends the stream or socket; disconnect() calls it.
DispatchResulttype{ ok: true; result: unknown } | { ok: false; denial?: HubDenial }: what dispatch() and dispatchAs() resolve to. denial is set when the guards refused.
NegotiateResponseinterface{ connectionId; connectionToken; negotiateVersion; availableTransports }.
SignalRMessageTypetype1 | 2 | 3 | 4 | 5 | 6 | 7.
MountedHubinterface{ path: string; hub: Hub; adapter: SignalRAdapter }.
HubRouterinterfaceThe router registerHubRoutes needs: get, post, optional ws.
HubRouteOptionsinterface{ webSockets?: () => boolean }: whether the server accepts WebSockets now; absent, SSE only.
HubHttpContextinterfaceThe request context the hub routes read.
HubSocketinterfaceThe WebSocket a hub writes to; matches ream's ServerWebSocket.
HubSseStreaminterfaceThe SSE stream a hub writes to.

Relay ​

MemberSignatureDescription
constructornew Relay(config?: RelayConfig)
authorizeauthorize<TParams extends Record<string, string> = Record<string, string>>(pattern: string, callback: ChannelAuthorizer<TParams>): voidWho may subscribe to channels matching pattern: static segments, :param, :param.suffix, a trailing optional :param?, a trailing *. An exact registration wins; patterns are tried in order.
registerRoutesregisterRoutes(customizer?: RelayRouteCustomizer): voidMounts GET /__relay/events, POST /__relay/subscribe and POST /__relay/unsubscribe, with customizer applied to each, and to the routes of every hub mounted afterwards (route.guard('web') to require an authenticated request, or route.middleware(...)). It does not replace a hub's useGuards(). The endpoints do not exist until it is called; call it from a preload.
hubhub(path: string, hub: Hub, adapter?: SignalRAdapter): voidTakes a Hub and, optionally, a SignalRAdapter built over it. A JavaScript caller that passes something else gets E_RELAY_INVALID_HUB or E_RELAY_INVALID_ADAPTER when the hub is mounted. Mounts a hub: POST <path>/negotiate, GET <path> (SSE), POST <path> and, when the server supports it, a WebSocket route on <path>. Mounting a path twice throws.
broadcastbroadcast(channel: string, payload: unknown): numberSends to every subscriber of channel; mirrored on the bus when there is one. Returns the local clients reached.
broadcastExceptbroadcastExcept(channel: string, payload: unknown, senderUid: string | string[]): numberSame, skipping the given connection ids on every instance.
onon<E extends keyof RelayLifecycleEvents>(event: E, listener: (evt: RelayLifecycleEvents[E]) => void): () => voidLifecycle listener; returns its detacher.
connectconnect(sse: RelaySseStream, ctx: RelayContext): { outcome: 'ok'; uid: string } | { outcome: 'capped' } | { outcome: 'owner-capped' } | { outcome: 'closed' }Registers an opened stream and issues its uid. Called by the events route.
subscribesubscribe(uid: unknown, channel: unknown, ctx: RelayContext): Promise<SubscribeResult>Called by the subscribe route.
unsubscribeunsubscribe(uid: unknown, channel: unknown, ctx: RelayContext): Promise<'ok' | 'forbidden'>Called by the unsubscribe route.
clientCountclientCount(): numberSSE clients on this instance.
channelSubscriberschannelSubscribers(channel: string): numberSubscribers of channel on this instance.
getSubscribersForgetSubscribersFor(channel: string): string[]Their uids.
startTransportstartTransport(): Promise<void>Joins the bus and starts the keep-alive; idempotent. The provider calls it in ready().
whenTransportReadywhenTransportReady(): Promise<void>Resolves once this instance listens to the bus; rejects if it never did.
shutdownshutdown(): Promise<void>Stops the keep-alive, ends every stream (hubs included) and leaves the bus.

The connection uid is issued by the server and sent to the client in the first connected frame; a subscribe must come from the user (and session) that opened the stream. A channel name is the SSE event name of its frames: control characters, connected, error and $$relay/ping are refused, and so is a name over 256 characters.

ts
interface RelayLifecycleEvents {
  connect: { uid: string; context: RelayContext }
  disconnect: { uid: string; context: RelayContext }
  subscribe: { uid: string; channel: string; context: RelayContext }
  unsubscribe: { uid: string; channel: string; context: RelayContext }
  broadcast: { channel: string; payload: unknown }
}

Hub ​

Extend it and write handlers as methods named on<Event>: onTaskUpdate handles taskUpdate, matched case-insensitively. Inherited handlers count; nothing on Hub itself is reachable from the client.

MemberSignatureDescription
useGuardsuseGuards(options: HubGuardOptions): voidWhat it takes to connect to the hub. Asked at negotiate, at the stream, at the WebSocket handshake and on every invocation. roles is met by any of the names, permissions by all of them.
authorizeauthorize(auth: HubAuth): Promise<HubDenial | null>Whether auth meets the guards: null when it does, always on a hub with none. The hub's routes call it. A permissions guard asks auth.hasAllPermissions when it is there, else reads auth.permissions or auth.user.permissions; an access token on auth.user.currentAccessToken must allow each permission too. An error of the auth layer propagates.
disconnectdisconnect(clientId: string): booleanEnds a connection held by this instance: off the hub at once, then its stream or socket is closed. false when there is no such client.
disconnectUserdisconnectUser(userId: string | number | bigint): numberEnds every connection this instance holds for that user; returns how many.
onConnectonConnect(ctx: HubContext): Promise<void>Override to react to a connection.
onDisconnectonDisconnect(clientId: string): Promise<void>Override to react to a disconnection.
dispatchdispatch(clientId: string, event: string, ...args: unknown[]): Promise<DispatchResult>Runs a handler against the connection's auth. { ok: true, result } or { ok: false }. When the guards refuse, the result carries denial and the client is taken off the hub: the transport closes the connection.
dispatchAsdispatchAs(clientId: string, auth: HubContext['auth'], event: string, args: readonly unknown[]): Promise<DispatchResult>Runs a handler against the auth of the request carrying the invocation.
registerClientregisterClient(client: ConnectedHubClient): HubContextRegisters a connection; the transport calls it, after authorize(). It does not ask the guards itself.
removeClientremoveClient(clientId: string): void
authForauthFor(clientId: string): HubContext['auth'] | undefinedThe auth recorded at connection.
statsstats(): { clients: number; groups: number }Clients and groups on this instance.
HubContext memberSignatureDescription
clientIdreadonly clientId: string
authreadonly auth: HubAuthThe invoking request's auth; the connection's in onConnect.
joinGroup / leaveGroupjoinGroup(group: string): void
sendsend(event: string, data: unknown): voidTo this client.
groupgroup(name: string): { send(event: string, data: unknown): void }To a group, on every instance.
broadcastbroadcast(event: string, data: unknown): voidTo every client of the hub, on every instance.
disconnectdisconnect(): voidEnds this connection.

A hub's guards are asked when a client connects and on each of its invocations, not while it only listens: end the connection of a user who logs out or loses a role with disconnectUser(). A connection opened with an access token is closed when user.currentAccessToken.expiresAt passes.

A hub connection lives on the instance that negotiated it: route a client's hub requests to one instance (sticky sessions on the hub paths).

SignalRAdapter ​

MemberSignatureDescription
constructornew SignalRAdapter(hub: Hub, options?)Options below.
negotiatenegotiate(connectionId: string, transports?: readonly string[], owner?: string, negotiator?: string): NegotiateResponseIssues a connection token; transports defaults to ['ServerSentEvents']. Throws when the pending table, or the owner's share, is full: check admits(owner) first.
admitsadmits(owner?: string): booleanWhether a negotiate from owner would be accepted.
handleFramehandleFrame(clientId: string, frame: string, auth?: HubContext['auth']): Promise<string[]>Processes an inbound frame; returns the frames to send back. The first frame must be the handshake, from a client the hub holds. Frames are parsed without __proto__ keys. An invocation the guards refuse is answered with a Close frame.
encodeInvocationencodeInvocation(target: string, args: unknown[]): stringA server-to-client invocation frame.
encodeCloseencodeClose(error?: string): stringA Close frame, sent before the server ends a connection.
resolveTokenresolveToken(connectionToken: string): string | undefined
forgetforget(clientId: string): void
OptionTypeDefaultDescription
maxFrameSizenumber65536Largest frame, in bytes.
tokenTtlMsnumber30000How long a negotiated token stays valid unclaimed.
maxPendingTokensnumber1024Unclaimed tokens waiting at once.
maxPendingTokensPerOwnernumber | false20Per user, or per client address when anonymous.
handshakeTimeoutMsnumber15000Time an opened stream has to complete the handshake.
maxConnectionsnumber10000Open streams; a new one past it gets 503.
maxConnectionsPerOwnernumber | false20Open streams per owner.
maxQueuedMessagesnumber256Messages one connection may have sent that the hub has not started. Past it a socket is closed with 1013 and an upstream POST gets 429. Every message counts, however many one WebSocket message or POST packs.

transports and RedisRelayTransport ​

ts
transports.redis(options: { connection: RelayPubSubResolver | string; owned?: boolean }): RelayTransportFactory
new RedisRelayTransport(client: RelayPubSubResolver, ownsConnection?: boolean)

connection takes a client, a function answering one, or the name of a @c9up/quasar connection. owned: true closes the client at shutdown; a named quasar connection is never closed by relay.

@c9up/relay/testing ​

ExportKindDescription
FakeRelayclassRecords broadcast and broadcastExcept calls, the payload as JSON would carry it. Both return 0.
CapturedBroadcastinterface{ channel: string; data: unknown; clients: number }.
FakeRelayPredicateinterface{ dataMatches?: (data: unknown) => boolean }.
FakeRelayPredicateArgtypeFakeRelayPredicate | ((event: CapturedBroadcast) => boolean).
FakeRelay memberSignatureDescription
broadcastbroadcast(channel: string, data: unknown): number
broadcastExceptbroadcastExcept(channel: string, data: unknown, senderUid: string | string[]): number
getSentgetSent(): CapturedBroadcast[]A copy of every captured broadcast.
resetreset(): void
assertSentassertSent(channel: string, predicate?: FakeRelayPredicateArg): voidThrows when no broadcast on channel matches.
assertNotSentassertNotSent(channel: string, predicate?: FakeRelayPredicateArg): voidThrows when one does.

Configuration ​

config/relay.ts exports defineConfig({ … }).

OptionTypeDefaultDescription
allowUnauthorizedChannelsbooleanfalseServe channels no authorize() covers. Off, they refuse subscribers.
maxClientsnumber10000SSE clients per instance.
maxClientsPerOwnernumber | false20SSE clients per user, or per client address when anonymous. Configure trusted proxies behind a proxy.
maxChannelsPerClientnumber100Channels one connection may hold.
transportRelayTransportSource | { driver: RelayTransportSource; channel?: string } | nullnoneThe bus between instances. Without one, a broadcast reaches the clients of the instance that made it only.
transportChannelstring'relay::broadcast'Bus channel, when transport is given as a bare driver; transport.channel wins.
pingIntervalnumber | string | falsefalseKeep-alive frames, in milliseconds or a duration ('30s'). The configure stub writes 30_000.

Container bindings ​

TokenResolves to
Relay (class), 'relay'The application's Relay, built from config/relay.ts.

'relay' is declared on ContainerBindings. The provider installs the route mounter in boot() and joins the bus in ready(); a failure to join fails the start.

Errors ​

CodeRaised when
E_RELAY_BAD_REQUESTsubscribe without a usable uid or channel, or with a refused channel name.
E_RELAY_CHANNEL_TOO_LONGThe channel name exceeds 256 characters.
E_RELAY_NOT_CONNECTEDNo connection has this uid here and no bus can reach the one holding it. Also a hub POST for a connection that has no open stream.
E_RELAY_BUS_UNAVAILABLEThe subscription had to cross the bus, and the bus refused it.
E_RELAY_NOT_OWNERThe connection was opened by another user.
E_RELAY_CHANNEL_NO_AUTHORIZERNo authorize() covers the channel.
E_RELAY_CHANNEL_FORBIDDENThe channel's authorizer refused, or threw.
E_RELAY_MAX_CHANNELSThe connection holds maxChannelsPerClient channels already.
E_RELAY_MAX_CLIENTSThe instance holds maxClients connections already.
E_RELAY_MAX_CLIENTS_PER_OWNERThis owner holds maxClientsPerOwner connections already.
E_RELAY_UNKNOWN_CONNECTIONA hub request with no valid connection token.
E_RELAY_MAX_CONNECTIONSA hub has maxConnections open streams.
E_RELAY_MAX_CONNECTIONS_PER_OWNERThis client has maxConnectionsPerOwner open hub streams.
E_RELAY_MAX_PENDINGToo many hub connections are negotiated and not yet open; SignalRAdapter.negotiate() throws it.
E_RELAY_MAX_QUEUEDA hub connection has maxQueuedMessages messages waiting already.
E_RELAY_SHUT_DOWNThe relay is shutting down.
E_RELAY_UNAUTHORIZEDA guarded hub needs an authenticated user, or one from another guard: 401 at negotiate, at the stream and at the WebSocket handshake, an error event on an invocation.
E_RELAY_FORBIDDENThe user lacks the role or the permissions a hub's guards require: 403 when connecting, an error event on an invocation.
E_RELAY_UNKNOWN_EVENTA hub invocation names no handler.
E_RELAY_HANDLER_ERRORA hub handler threw.
E_RELAY_INVALID_CHANNEL_PATTERNauthorize() was given a channel pattern that could never match.
E_RELAY_INVALID_PING_INTERVALpingInterval is neither milliseconds nor a duration like '30s'.
E_RELAY_HUB_PATH_TAKENrelay.hub() names a path where a hub is already mounted.
E_RELAY_HUB_ALREADY_MOUNTEDOne hub instance is mounted a second time; give each mount its own instance.
E_RELAY_DUPLICATE_HANDLERTwo handlers of a hub differ only by case, so they answer the same invocation.
E_RELAY_INVALID_HUBThe object mounted with relay.hub() does not extend Hub.
E_RELAY_INVALID_ADAPTERThe adapter given to relay.hub() is not a SignalRAdapter.
E_RELAY_TRANSPORT_NOT_STARTEDwhenTransportReady() was called before startTransport().
E_RELAY_TRANSPORT_SUPERSEDEDA startTransport() was overtaken by a shutdown or a newer start before it subscribed.
E_RELAY_TRANSPORT_UNRELEASEDstartTransport() could not release the previous bus subscription, so it refuses to subscribe again; cause is the bus's error.
E_RELAY_QUASAR_MISSINGA Redis bus names a quasar connection and @c9up/quasar is not installed, or its provider has not booted.
E_RELAY_REDIS_INCOMPLETEThe quasar connection lacks a command the bus issues.
E_RELAY_SUBSCRIBE_FAILEDThe bus client's subscribe failed with something other than an Error; cause holds what it threw.
E_RELAY_ASSERTION_FAILEDFakeRelay.assertSent() or assertNotSent() does not hold.
E_RELAY_INTERNALAn invariant of relay broke. A bug: report it.

Migration ​

  • useGuards() on a hub is now a requirement to connect, not only to invoke. A client the guards refuse gets 401 or 403 at <path>/negotiate, at the stream and at the WebSocket handshake; it used to connect, run onConnect and receive every broadcast. A hub that anyone may listen to and only some may call: remove useGuards() and check ctx.auth in the methods to protect.
  • An invocation the guards refuse now closes the connection, after the error event and the error Completion. A role granted after the connection opened no longer starts working mid-connection: the client reconnects.
  • A hub's permissions guard now asks ctx.auth.hasAllPermissions() when the auth layer has it (Warden's rights resolver) instead of reading the permission list of the user payload, and an access token must allow each permission too. Grant the permission in the rights store, and give the token the ability.
  • The message of E_RELAY_UNAUTHORIZED for a wrong guard no longer lists the accepted guards. Test code, not the message.
  • A hub connection opened with an access token is closed when the token expires. Have the client reconnect with a fresh token (withAutomaticReconnect()).
  • A hub connection whose onConnect throws is closed. Over Server-Sent Events it used to stay open and receive broadcasts.
  • A hub connection with more than maxQueuedMessages messages waiting (256) is closed with 1013, or its POST refused with 429. Raise the option on the SignalRAdapter for a client that sends many invocations without awaiting them.
  • A hub POST sent before its stream is open now answers 400 E_RELAY_NOT_CONNECTED; it used to answer 200 and drop the replies. Open the stream first, as the SignalR client does.
  • The invocations of one hub connection run one after another over Server-Sent Events too, in the order they arrive. Concurrent POSTs on one connection used to run in parallel.
  • Hub invocation arguments no longer carry a __proto__ key, nor a constructor object that has prototype.
  • Hub has three new methods, authorize, disconnect and disconnectUser, and HubContext has disconnect. A hub subclass that declares a method with one of these names must rename it.
  • relay.hub() takes a Hub and a SignalRAdapter, no longer any object. TypeScript now refuses an object that is not a Hub at the call site; until now it compiled and failed with E_RELAY_INVALID_HUB at mount time. Pass an instance of a class that extends Hub.
  • RelayError and RelayTypeError are gone: test error.code, or error instanceof errors.E_RELAY_* with import { errors } from '@c9up/relay'. E_RELAY_INVALID_HUB and E_RELAY_INVALID_ADAPTER are no longer a TypeError.

See also ​