API reference / @chanx-js/client / core / ChannelConnection
Class: ChannelConnection<ToServer, ToClient>
Defined in: packages/client/src/core/connection.ts:50
A typed channel riding a shared socket.
Routes each inbound frame exactly once: a pending request claims it by ref, else a topic claims it by topic, else it belongs to the channel itself. Framework frames (complete, subscribed, error) are absorbed here so they never reach user code.
Type Parameters
| Type Parameter | Default type |
|---|---|
ToServer extends ChanxMessage | ChanxMessage |
ToClient extends ChanxMessage | ChanxMessage |
Implements
TopicHost
Constructors
Constructor
new ChannelConnection<ToServer, ToClient>(
socket,
descriptor,
validate
): ChannelConnection<ToServer, ToClient>;Defined in: packages/client/src/core/connection.ts:75
Parameters
| Parameter | Type |
|---|---|
socket | SharedSocket |
descriptor | ChannelDescriptor<ToServer, ToClient, string, any> |
validate | { inbound: boolean; outbound: boolean; } |
validate.inbound | boolean |
validate.outbound | boolean |
Returns
ChannelConnection<ToServer, ToClient>
Accessors
status
Get Signature
get status(): SocketStatus;Defined in: packages/client/src/core/connection.ts:90
Returns
url
Get Signature
get url(): string;Defined in: packages/client/src/core/connection.ts:94
Returns
string
Methods
[asyncIterator]()
asyncIterator: AsyncIterableIterator<ToClient>;Defined in: packages/client/src/core/connection.ts:138
Returns
AsyncIterableIterator<ToClient>
claimSubscription()
claimSubscription(topic): void;Defined in: packages/client/src/core/connection.ts:228
Internal
TopicHost
Parameters
| Parameter | Type |
|---|---|
topic | string |
Returns
void
Implementation of
TopicHost.claimSubscriptionclose()
close(): void;Defined in: packages/client/src/core/connection.ts:257
Returns
void
on()
on<A>(action, handler): () => void;Defined in: packages/client/src/core/connection.ts:143
envelope holds the frame's routing fields, such as a broadcast's seq.
Type Parameters
| Type Parameter |
|---|
A extends string |
Parameters
| Parameter | Type |
|---|---|
action | A |
handler | (message, envelope) => void |
Returns
() => void
onAny()
onAny(handler): () => void;Defined in: packages/client/src/core/connection.ts:150
Parameters
| Parameter | Type |
|---|---|
handler | (message, envelope) => void |
Returns
() => void
once()
once<A>(action, options?): Promise<Extract<ToClient, {
action: A;
}>>;Defined in: packages/client/src/core/connection.ts:120
Resolve with the next message carrying this action.
Type Parameters
| Type Parameter |
|---|
A extends string |
Parameters
| Parameter | Type |
|---|---|
action | A |
options | { signal?: AbortSignal; timeout?: number; } |
options.signal? | AbortSignal |
options.timeout? | number |
Returns
Promise<Extract<ToClient, { action: A; }>>
onComplete()
onComplete(handler): () => void;Defined in: packages/client/src/core/connection.ts:166
complete / event_complete / group_complete, if the server sends them.
Parameters
| Parameter | Type |
|---|---|
handler | (message) => void |
Returns
() => void
onError()
onError(handler): () => void;Defined in: packages/client/src/core/connection.ts:160
Server-side error frames, which chanx sends in place of the expected reply.
Parameters
| Parameter | Type |
|---|---|
handler | (error) => void |
Returns
() => void
onStatus()
onStatus(handler): () => void;Defined in: packages/client/src/core/connection.ts:98
Parameters
| Parameter | Type |
|---|---|
handler | (status) => void |
Returns
() => void
onUnhandled()
onUnhandled(handler): () => void;Defined in: packages/client/src/core/connection.ts:155
Messages that arrived with no handler for their action.
Parameters
| Parameter | Type |
|---|---|
handler | (message, envelope) => void |
Returns
() => void
ready()
ready(options?): Promise<void>;Defined in: packages/client/src/core/connection.ts:108
Resolve once the socket is open.
Sends are queued until then, so this is only needed when a script wants to fail fast on an unreachable server rather than buffer.
Parameters
| Parameter | Type |
|---|---|
options | { signal?: AbortSignal; timeout?: number; } |
options.signal? | AbortSignal |
options.timeout? | number |
Returns
Promise<void>
releaseSubscription()
releaseSubscription(topic): boolean;Defined in: packages/client/src/core/connection.ts:233
Internal
TopicHost
Parameters
| Parameter | Type |
|---|---|
topic | string |
Returns
boolean
Implementation of
TopicHost.releaseSubscriptionreleaseTopic()
releaseTopic(topic): void;Defined in: packages/client/src/core/connection.ts:217
Internal
TopicHost
Parameters
| Parameter | Type |
|---|---|
topic | string |
Returns
void
Implementation of
TopicHost.releaseTopicrequest()
request(message, options?): Promise<ChanxMessage>;Defined in: packages/client/src/core/connection.ts:183
Send and wait for the frame carrying the same ref.
Resolves on the first matching frame. Needs chanx 2.11.2+, the first release to echo the ref on replies to untopiced frames; earlier servers time out here.
Parameters
| Parameter | Type |
|---|---|
message | ToServer |
options | RequestOptions |
Returns
Promise<ChanxMessage>
requestTopic()
requestTopic(
topic,
message,
timeout?
): Promise<ChanxMessage>;Defined in: packages/client/src/core/connection.ts:212
Internal
TopicHost
Parameters
| Parameter | Type |
|---|---|
topic | string |
message | object |
timeout? | number |
Returns
Promise<ChanxMessage>
Implementation of
TopicHost.requestTopicsend()
send(message): void;Defined in: packages/client/src/core/connection.ts:172
Throws when outbound validation rejects the message: that is a caller bug.
Parameters
| Parameter | Type |
|---|---|
message | ToServer |
Returns
void
sendTopic()
sendTopic(topic, message): void;Defined in: packages/client/src/core/connection.ts:207
Internal
TopicHost
Parameters
| Parameter | Type |
|---|---|
topic | string |
message | object |
Returns
void
Implementation of
TopicHost.sendTopicstream()
stream(options?): MessageStream<ToClient>;Defined in: packages/client/src/core/connection.ts:134
Iterate messages with for await. Ends when the connection closes or the signal aborts.
Parameters
| Parameter | Type |
|---|---|
options | StreamOptions |
Returns
MessageStream<ToClient>
terminate()
terminate(code?, reason?): void;Defined in: packages/client/src/core/connection.ts:253
Close the socket for every consumer sharing it, not just this one.
close() gives up this consumer's claim and leaves the socket to the others; this ends it outright, e.g. on logout or when the server-side identity changed. The next connect() to the same URL opens a fresh socket.
Parameters
| Parameter | Type | Default value |
|---|---|---|
code | number | 1000 |
reason | string | '' |
Returns
void
topic()
topic<TS, TC>(ref): TopicHandle<TS, TC>;Defined in: packages/client/src/core/connection.ts:193
Take a handle on a topic: connection.topic(roomTopic.with({ room_name: 'lobby' })). Handles are shared per resolved topic string and reference counted, so two components wanting room:lobby get one subscription.
Type Parameters
| Type Parameter |
|---|
TS extends ChanxMessage |
TC extends ChanxMessage |
Parameters
| Parameter | Type |
|---|---|
ref | TopicRef<TS, TC, string> |
Returns
TopicHandle<TS, TC>