Skip to content

Class: KyooConnection<T> ​

The main class for interacting with a Queue Broker.

Remarks ​

This class is used to create a connection to a Queue Broker and manage queues.

Type Parameters ​

Type ParameterDescription
T extends KyooClientBroker client: amqp, bullmq, sqs, pgboss, or sql.

Constructors ​

Constructor ​

ts
new KyooConnection<T>(config: KyooConfiguration<T>): KyooConnection<T>;

Parameters ​

ParameterType
configKyooConfiguration<T>

Returns ​

KyooConnection<T>

Accessors ​

connectionStatus ​

Get Signature ​

ts
get connectionStatus(): ConnectionStatus;

Reports the status currently supplied by the adapter.

Returns ​

ConnectionStatus

Connected, disconnected, or connecting status.


queues ​

Get Signature ​

ts
get queues(): {
  destroy: (name: string) => Promise<void>;
  get: (name: string) => KyooQueue<T>;
  terminate: (name?: string) => Promise<any>;
  get count(): any;
};

Returns a frozen object for interacting with queues. get(name) returns a queue handle for the supplied name, and count returns the adapter-reported number of managed queues. terminate(name?) emits a termination signal and terminates the named queue, or all queues when name is omitted. destroy(name) removes the named queue from the broker.

Returns ​

A frozen collection of queue lookup, counting, termination, and destruction operations.

NameTypeDescription
destroy()(name: string) => Promise<void>Remove a queue from the queue broker
get()(name: string) => KyooQueue<T>Retrieve a queue by name
terminate()(name?: string) => Promise<any>Terminate a queue or all queues
get countanyGet the number of queues managed by this connection

version ​

Get Signature ​

ts
get version(): string;

The version of the library

Returns ​

string

The version of the library


active ​

Get Signature ​

ts
get static active(): number;

Counts connection instances currently registered in this process.

Returns ​

number

Number of known connections, including disconnected instances not terminated.


connected ​

Get Signature ​

ts
get static connected(): number;

Counts registered connections whose adapters report a connected status.

Returns ​

number

Number of currently connected registered instances.


connecting ​

Get Signature ​

ts
get static connecting(): number;

Counts registered connections whose adapters are still connecting.

Returns ​

number

Number of registered instances with an in-progress connection attempt.


disconnected ​

Get Signature ​

ts
get static disconnected(): number;

Counts registered connections whose adapters report a disconnected status.

Returns ​

number

Number of currently disconnected registered instances.


version ​

Get Signature ​

ts
get static version(): string;

The version of the library

Returns ​

string

The version of the library

Methods ​

addListener() ​

ts
addListener<K>(eventName: K, listener: KyooConnectionToApplicationEventEmitterListener<K>): this;

Registers an event listener; equivalent to KyooConnection.on.

Type Parameters ​

Type Parameter
K extends keyof KyooConnectionEvents

Parameters ​

ParameterTypeDescription
eventNameKApplication event to observe.
listenerKyooConnectionToApplicationEventEmitterListener<K>Callback invoked when the event is emitted.

Returns ​

this

This connection for chaining.


connect() ​

ts
connect(signal?: AbortSignal): Promise<boolean>;

Establishes a connection to the configured queue broker.

Parameters ​

ParameterTypeDescription
signal?AbortSignalOptional signal forwarded to the adapter to cancel the connection attempt.

Returns ​

Promise<boolean>

True when the adapter establishes a broker connection; otherwise false.

Remarks ​

Can be used to reconnect to the Queue Broker after being disconnected


disconnect() ​

ts
disconnect(terminate: boolean): Promise<void>;

Disconnects from the configured queue broker.

Parameters ​

ParameterTypeDefault valueDescription
terminatebooleanfalseWhether to release all queue subscriptions; defaults to false.

Returns ​

Promise<void>

Remarks ​

When terminate is false, the connection will be closed, but queue subscriptions will remain. This is useful for cases where you may want to reconnect without having to resubscribe to all queues. When terminate is true, the connection will be closed and all queue subscriptions will be released. This is useful for cases where you want to clean up all resources, usually before shutting down the application.


off() ​

ts
off<K>(eventName: K, listener: KyooConnectionToApplicationEventEmitterListener<K>): this;

Removes one registration of a listener; equivalent to removeListener.

Type Parameters ​

Type Parameter
K extends keyof KyooConnectionEvents

Parameters ​

ParameterTypeDescription
eventNameKApplication event whose listener should be removed.
listenerKyooConnectionToApplicationEventEmitterListener<K>Registered callback to remove.

Returns ​

this

This connection for chaining.


on() ​

ts
on<K>(eventName: K, listener: KyooConnectionToApplicationEventEmitterListener<K>): this;

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the KyooConnection, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The KyooConnection.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

Type Parameters ​

Type Parameter
K extends keyof KyooConnectionEvents

Parameters ​

ParameterTypeDescription
eventNameKThe name of the event.
listenerKyooConnectionToApplicationEventEmitterListener<K>The callback function

Returns ​

this

This connection for chaining.


once() ​

ts
once<K>(eventName: K, listener: KyooConnectionToApplicationEventEmitterListener<K>): this;

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the KyooConnection, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The KyooConnection.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

Type Parameters ​

Type Parameter
K extends keyof KyooConnectionEvents

Parameters ​

ParameterTypeDescription
eventNameKThe name of the event.
listenerKyooConnectionToApplicationEventEmitterListener<K>The callback function

Returns ​

this

This connection for chaining.


prependListener() ​

ts
prependListener<K>(eventName: K, listener: KyooConnectionToApplicationEventEmitterListener<K>): this;

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the KyooConnection, so that calls can be chained.

Type Parameters ​

Type Parameter
K extends keyof KyooConnectionEvents

Parameters ​

ParameterTypeDescription
eventNameKThe name of the event.
listenerKyooConnectionToApplicationEventEmitterListener<K>The callback function

Returns ​

this

This connection for chaining.


prependOnceListener() ​

ts
prependOnceListener<K>(eventName: K, listener: KyooConnectionToApplicationEventEmitterListener<K>): this;

Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the KyooConnection, so that calls can be chained.

Type Parameters ​

Type Parameter
K extends keyof KyooConnectionEvents

Parameters ​

ParameterTypeDescription
eventNameKThe name of the event.
listenerKyooConnectionToApplicationEventEmitterListener<K>The callback function

Returns ​

this

This connection for chaining.


removeAllListeners() ​

ts
removeAllListeners<K>(eventName?: K): this;

Removes all listeners, or those of the specified eventName.

It is bad practice to remove listeners added elsewhere in the code, particularly when the KyooConnection instance was created by some other component or module.

Returns a reference to the KyooConnection, so that calls can be chained.

Type Parameters ​

Type Parameter
K extends keyof KyooConnectionEvents

Parameters ​

ParameterTypeDescription
eventName?KOptional event whose listeners to clear; omit to clear every event.

Returns ​

this

This connection for chaining.


removeListener() ​

ts
removeListener<K>(eventName: K, listener: KyooConnectionToApplicationEventEmitterListener<K>): this;

Removes the specified listener from the listener array for the event named eventName.

removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() or removeAllListeners() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events behave as expected.

Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called.

When a single function has been added as a handler multiple times for a single event, removeListener() will remove the most recently added instance.

Returns a reference to the KyooConnection, so that calls can be chained.

Type Parameters ​

Type Parameter
K extends keyof KyooConnectionEvents

Parameters ​

ParameterTypeDescription
eventNameKEvent whose listener should be removed.
listenerKyooConnectionToApplicationEventEmitterListener<K>One registration of the callback to remove.

Returns ​

this

This connection for chaining.


cleanup() ​

ts
static cleanup(): Promise<void>;

Disconnects and terminates all known KyooConnections

Returns ​

Promise<void>

A promise fulfilled after all registered connections have been asked to terminate.

Remarks ​

This is useful for cleaning up all resources before shutting down the application.