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 KyooClientThe type of the Queue Broker client. Options are amqp, bullmq and sqs.

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;

The connection status

Returns

ConnectionStatus

"connected" if the connection is established, disconnected if not, connecting if the connection is in progress


queues

Get Signature

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

An object of methods for interacting with queues

Returns
ts
{
  destroy: (name: string) => Promise<void>;
  get: (name: string) => KyooQueue<T>;
  terminate: (name?: string) => Promise<any>;
  get count
  count: any;
}
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;

Returns the count of known KyooConnections

Returns

number


connected

Get Signature

ts
get static connected(): number;

Returns the list of known KyooConnections which have the status of "connected"

Returns

number


connecting

Get Signature

ts
get static connecting(): number;

Returns the list of known KyooConnections which have the status of "connecting"

Returns

number


disconnected

Get Signature

ts
get static disconnected(): number;

Returns the list of known KyooConnections which have the status of "disconnected"

Returns

number


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;

Alias for KyooConnection.on(eventName, listener).

Type Parameters

Type Parameter
K extends keyof KyooConnectionEvents

Parameters

ParameterType
eventNameK
listenerKyooConnectionToApplicationEventEmitterListener<K>

Returns

this


connect()

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

Connect to the Queue Broker

Parameters

ParameterTypeDescription
signal?AbortSignalAn optional AbortSignal to cancel the connection

Returns

Promise<boolean>

Remarks

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


disconnect()

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

Disconnect from the Queue Broker

Parameters

ParameterTypeDefault valueDescription
terminatebooleanfalseWhether to release all queue subscriptions

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;

Alias for KyooConnection.removeListener().

Type Parameters

Type Parameter
K extends keyof KyooConnectionEvents

Parameters

ParameterType
eventNameK
listenerKyooConnectionToApplicationEventEmitterListener<K>

Returns

this


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


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


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


prependOnceListener()

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

Adds a one-timelistener 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


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

ParameterType
eventName?K

Returns

this


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 fromemit() 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

ParameterType
eventNameK
listenerKyooConnectionToApplicationEventEmitterListener<K>

Returns

this


cleanup()

ts
static cleanup(): Promise<void>;

Disconnects and terminates all known KyooConnections

Returns

Promise<void>

Remarks

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