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 | The type of the Queue Broker client. Options are amqp, bullmq and sqs. |
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;The connection status
Returns
"connected" if the connection is established, disconnected if not, connecting if the connection is in progress
queues
Get Signature
get queues(): {
destroy: (name: string) => Promise<void>;
get: (name: string) => KyooQueue<T>;
terminate: (name?: string) => Promise<any>;
get count(): any;
};An object of methods for interacting with queues
Returns
| 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;Returns the count of known KyooConnections
Returns
number
connected
Get Signature
get static connected(): number;Returns the list of known KyooConnections which have the status of "connected"
Returns
number
connecting
Get Signature
get static connecting(): number;Returns the list of known KyooConnections which have the status of "connecting"
Returns
number
disconnected
Get Signature
get static disconnected(): number;Returns the list of known KyooConnections which have the status of "disconnected"
Returns
number
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;Alias for KyooConnection.on(eventName, listener).
Type Parameters
| Type Parameter |
|---|
K extends keyof KyooConnectionEvents |
Parameters
| Parameter | Type |
|---|---|
eventName | K |
listener | KyooConnectionToApplicationEventEmitterListener<K> |
Returns
this
connect()
connect(signal?: AbortSignal): Promise<boolean>;Connect to the Queue Broker
Parameters
| Parameter | Type | Description |
|---|---|---|
signal? | AbortSignal | An optional AbortSignal to cancel the connection |
Returns
Promise<boolean>
Remarks
Can be used to reconnect to the Queue Broker after being disconnected
disconnect()
disconnect(terminate: boolean): Promise<void>;Disconnect from the Queue Broker
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
terminate | boolean | false | Whether 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()
off<K>(eventName: K, listener: KyooConnectionToApplicationEventEmitterListener<K>): this;Alias for KyooConnection.removeListener().
Type Parameters
| Type Parameter |
|---|
K extends keyof KyooConnectionEvents |
Parameters
| Parameter | Type |
|---|---|
eventName | K |
listener | KyooConnectionToApplicationEventEmitterListener<K> |
Returns
this
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
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
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
prependOnceListener()
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
| Parameter | Type | Description |
|---|---|---|
eventName | K | The name of the event. |
listener | KyooConnectionToApplicationEventEmitterListener<K> | The callback function |
Returns
this
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 |
|---|---|
eventName? | K |
Returns
this
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 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
| Parameter | Type |
|---|---|
eventName | K |
listener | KyooConnectionToApplicationEventEmitterListener<K> |
Returns
this
cleanup()
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.
