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 Parameter | Description |
|---|---|
T extends KyooClient | Broker client: amqp, bullmq, sqs, pgboss, or sql. |
Constructors
Constructor
new KyooConnection<T>(config: KyooConfiguration<T>): KyooConnection<T>;Parameters
| Parameter | Type |
|---|---|
config | KyooConfiguration<T> |
Returns
KyooConnection<T>
Accessors
connectionStatus
Get Signature
get connectionStatus(): ConnectionStatus;Reports the status currently supplied by the adapter.
Returns
Connected, disconnected, or connecting status.
queues
Get Signature
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.
| Name | Type | Description |
|---|---|---|
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 count | any | Get the number of queues managed by this connection |
version
Get Signature
get version(): string;The version of the library
Returns
string
The version of the library
active
Get Signature
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
get static connected(): number;Counts registered connections whose adapters report a connected status.
Returns
number
Number of currently connected registered instances.
connecting
Get Signature
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
get static disconnected(): number;Counts registered connections whose adapters report a disconnected status.
Returns
number
Number of currently disconnected registered instances.
version
Get Signature
get static version(): string;The version of the library
Returns
string
The version of the library
Methods
addListener()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | Application event to observe. |
listener | KyooConnectionToApplicationEventEmitterListener<K> | Callback invoked when the event is emitted. |
Returns
this
This connection for chaining.
connect()
connect(signal?: AbortSignal): Promise<boolean>;Establishes a connection to the configured queue broker.
Parameters
| Parameter | Type | Description |
|---|---|---|
signal? | AbortSignal | Optional 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()
disconnect(terminate: boolean): Promise<void>;Disconnects from the configured queue broker.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
terminate | boolean | false | Whether 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()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | Application event whose listener should be removed. |
listener | KyooConnectionToApplicationEventEmitterListener<K> | Registered callback to remove. |
Returns
this
This connection for chaining.
on()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooConnectionToApplicationEventEmitterListener<K> | The callback function |
Returns
this
This connection for chaining.
once()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooConnectionToApplicationEventEmitterListener<K> | The callback function |
Returns
this
This connection for chaining.
prependListener()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooConnectionToApplicationEventEmitterListener<K> | The callback function |
Returns
this
This connection for chaining.
prependOnceListener()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooConnectionToApplicationEventEmitterListener<K> | The callback function |
Returns
this
This connection for chaining.
removeAllListeners()
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
| Parameter | Type | Description |
|---|---|---|
eventName? | K | Optional event whose listeners to clear; omit to clear every event. |
Returns
this
This connection for chaining.
removeListener()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | Event whose listener should be removed. |
listener | KyooConnectionToApplicationEventEmitterListener<K> | One registration of the callback to remove. |
Returns
this
This connection for chaining.
cleanup()
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.
