API reference / @chanx-js/client / core / ChannelConnection
Class: ChannelConnection<ToServer, ToClient, Replies>
Defined in: packages/client/src/core/connection.ts:75
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 |
Replies | NoReplies |
Implements
TopicHost
Constructors
Constructor
new ChannelConnection<ToServer, ToClient, Replies>(
socket,
descriptor,
validate
): ChannelConnection<ToServer, ToClient, Replies>;Defined in: packages/client/src/core/connection.ts:101
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, Replies>
Accessors
status
Get Signature
get status(): SocketStatus;Defined in: packages/client/src/core/connection.ts:123
Returns
url
Get Signature
get url(): string;Defined in: packages/client/src/core/connection.ts:127
Returns
string
Methods
[asyncIterator]()
asyncIterator: AsyncIterableIterator<ToClient>;Defined in: packages/client/src/core/connection.ts:177
Returns
AsyncIterableIterator<ToClient>
claimSubscription()
claimSubscription(topic): void;Defined in: packages/client/src/core/connection.ts:273
Internal
TopicHost
Parameters
| Parameter | Type |
|---|---|
topic | string |
Returns
void
Implementation of
TopicHost.claimSubscriptionclose()
close(): void;Defined in: packages/client/src/core/connection.ts:302
Returns
void
on()
on<A>(action, handler): () => void;Defined in: packages/client/src/core/connection.ts:182
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:189
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:159
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:205
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:199
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:131
Parameters
| Parameter | Type |
|---|---|
handler | (status) => void |
Returns
() => void
onUnhandled()
onUnhandled(handler): () => void;Defined in: packages/client/src/core/connection.ts:194
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:142
Resolve once the socket is open; reject once it has given up, after its reconnect attempts, or was refused or terminated.
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:278
Internal
TopicHost
Parameters
| Parameter | Type |
|---|---|
topic | string |
Returns
boolean
Implementation of
TopicHost.releaseSubscriptionreleaseTopic()
releaseTopic(topic): void;Defined in: packages/client/src/core/connection.ts:262
Internal
TopicHost
Parameters
| Parameter | Type |
|---|---|
topic | string |
Returns
void
Implementation of
TopicHost.releaseTopicrequest()
request<A>(message, options?): Promise<ReplyOf<Replies, A>>;Defined in: packages/client/src/core/connection.ts:222
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.
Type Parameters
| Type Parameter |
|---|
A extends string |
Parameters
| Parameter | Type |
|---|---|
message | ToServer & object |
options | RequestOptions |
Returns
Promise<ReplyOf<Replies, A>>
requestTopic()
requestTopic(
topic,
message,
timeout?
): Promise<ChanxMessage>;Defined in: packages/client/src/core/connection.ts:257
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:211
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:252
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:173
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:298
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, R>(ref): TopicHandle<TS, TC, R>;Defined in: packages/client/src/core/connection.ts:238
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 | Default type |
|---|---|
TS extends ChanxMessage | - |
TC extends ChanxMessage | - |
R | NoReplies |
Parameters
| Parameter | Type |
|---|---|
ref | TopicRef<TS, TC, string, R> |
Returns
TopicHandle<TS, TC, R>