Skip to content

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 ParameterDefault type
ToServer extends ChanxMessageChanxMessage
ToClient extends ChanxMessageChanxMessage
RepliesNoReplies

Implements ​

  • TopicHost

Constructors ​

Constructor ​

ts
new ChannelConnection<ToServer, ToClient, Replies>(
   socket, 
   descriptor, 
   validate
): ChannelConnection<ToServer, ToClient, Replies>;

Defined in: packages/client/src/core/connection.ts:101

Parameters ​

ParameterType
socketSharedSocket
descriptorChannelDescriptor<ToServer, ToClient, string, any>
validate{ inbound: boolean; outbound: boolean; }
validate.inboundboolean
validate.outboundboolean

Returns ​

ChannelConnection<ToServer, ToClient, Replies>

Accessors ​

status ​

Get Signature ​

ts
get status(): SocketStatus;

Defined in: packages/client/src/core/connection.ts:123

Returns ​

SocketStatus


url ​

Get Signature ​

ts
get url(): string;

Defined in: packages/client/src/core/connection.ts:127

Returns ​

string

Methods ​

[asyncIterator]() ​

ts
asyncIterator: AsyncIterableIterator<ToClient>;

Defined in: packages/client/src/core/connection.ts:177

Returns ​

AsyncIterableIterator<ToClient>


claimSubscription() ​

ts
claimSubscription(topic): void;

Defined in: packages/client/src/core/connection.ts:273

Internal

TopicHost

Parameters ​

ParameterType
topicstring

Returns ​

void

Implementation of ​

ts
TopicHost.claimSubscription

close() ​

ts
close(): void;

Defined in: packages/client/src/core/connection.ts:302

Returns ​

void


on() ​

ts
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 ​

ParameterType
actionA
handler(message, envelope) => void

Returns ​

() => void


onAny() ​

ts
onAny(handler): () => void;

Defined in: packages/client/src/core/connection.ts:189

Parameters ​

ParameterType
handler(message, envelope) => void

Returns ​

() => void


once() ​

ts
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 ​

ParameterType
actionA
options{ signal?: AbortSignal; timeout?: number; }
options.signal?AbortSignal
options.timeout?number

Returns ​

Promise<Extract<ToClient, { action: A; }>>


onComplete() ​

ts
onComplete(handler): () => void;

Defined in: packages/client/src/core/connection.ts:205

complete / event_complete / group_complete, if the server sends them.

Parameters ​

ParameterType
handler(message) => void

Returns ​

() => void


onError() ​

ts
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 ​

ParameterType
handler(error) => void

Returns ​

() => void


onStatus() ​

ts
onStatus(handler): () => void;

Defined in: packages/client/src/core/connection.ts:131

Parameters ​

ParameterType
handler(status) => void

Returns ​

() => void


onUnhandled() ​

ts
onUnhandled(handler): () => void;

Defined in: packages/client/src/core/connection.ts:194

Messages that arrived with no handler for their action.

Parameters ​

ParameterType
handler(message, envelope) => void

Returns ​

() => void


ready() ​

ts
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 ​

ParameterType
options{ signal?: AbortSignal; timeout?: number; }
options.signal?AbortSignal
options.timeout?number

Returns ​

Promise<void>


releaseSubscription() ​

ts
releaseSubscription(topic): boolean;

Defined in: packages/client/src/core/connection.ts:278

Internal

TopicHost

Parameters ​

ParameterType
topicstring

Returns ​

boolean

Implementation of ​

ts
TopicHost.releaseSubscription

releaseTopic() ​

ts
releaseTopic(topic): void;

Defined in: packages/client/src/core/connection.ts:262

Internal

TopicHost

Parameters ​

ParameterType
topicstring

Returns ​

void

Implementation of ​

ts
TopicHost.releaseTopic

request() ​

ts
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 ​

ParameterType
messageToServer & object
optionsRequestOptions

Returns ​

Promise<ReplyOf<Replies, A>>


requestTopic() ​

ts
requestTopic(
   topic, 
   message, 
   timeout?
): Promise<ChanxMessage>;

Defined in: packages/client/src/core/connection.ts:257

Internal

TopicHost

Parameters ​

ParameterType
topicstring
messageobject
timeout?number

Returns ​

Promise<ChanxMessage>

Implementation of ​

ts
TopicHost.requestTopic

send() ​

ts
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 ​

ParameterType
messageToServer

Returns ​

void


sendTopic() ​

ts
sendTopic(topic, message): void;

Defined in: packages/client/src/core/connection.ts:252

Internal

TopicHost

Parameters ​

ParameterType
topicstring
messageobject

Returns ​

void

Implementation of ​

ts
TopicHost.sendTopic

stream() ​

ts
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 ​

ParameterType
optionsStreamOptions

Returns ​

MessageStream<ToClient>


terminate() ​

ts
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 ​

ParameterTypeDefault value
codenumber1000
reasonstring''

Returns ​

void


topic() ​

ts
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 ParameterDefault type
TS extends ChanxMessage-
TC extends ChanxMessage-
RNoReplies

Parameters ​

ParameterType
refTopicRef<TS, TC, string, R>

Returns ​

TopicHandle<TS, TC, R>

MIT licensed