Skip to content

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

Implements ​

  • TopicHost

Constructors ​

Constructor ​

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

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

Parameters ​

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

Returns ​

ChannelConnection<ToServer, ToClient>

Accessors ​

status ​

Get Signature ​

ts
get status(): SocketStatus;

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

Returns ​

SocketStatus


url ​

Get Signature ​

ts
get url(): string;

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

Returns ​

string

Methods ​

[asyncIterator]() ​

ts
asyncIterator: AsyncIterableIterator<ToClient>;

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

Returns ​

AsyncIterableIterator<ToClient>


claimSubscription() ​

ts
claimSubscription(topic): void;

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

Internal

TopicHost

Parameters ​

ParameterType
topicstring

Returns ​

void

Implementation of ​

ts
TopicHost.claimSubscription

close() ​

ts
close(): void;

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

Returns ​

void


on() ​

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

ParameterType
actionA
handler(message, envelope) => void

Returns ​

() => void


onAny() ​

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

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

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:120

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:166

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:160

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:98

Parameters ​

ParameterType
handler(status) => void

Returns ​

() => void


onUnhandled() ​

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

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

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

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:233

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:217

Internal

TopicHost

Parameters ​

ParameterType
topicstring

Returns ​

void

Implementation of ​

ts
TopicHost.releaseTopic

request() ​

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

ParameterType
messageToServer
optionsRequestOptions

Returns ​

Promise<ChanxMessage>


requestTopic() ​

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

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

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:172

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:207

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:134

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

ParameterTypeDefault value
codenumber1000
reasonstring''

Returns ​

void


topic() ​

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

ParameterType
refTopicRef<TS, TC, string>

Returns ​

TopicHandle<TS, TC>

MIT licensed