UNPKG

rabbitmq-client

Version:
194 lines (193 loc) 9.95 kB
import EventEmitter from 'node:events'; import { AsyncMessage, Cmd, Envelope, MessageBody, MethodParams, ReturnedMessage, SyncMessage } from './codec'; export declare interface Channel { /** The specified consumer was stopped by the server. The error param * describes the reason for the cancellation. */ on(name: 'basic.cancel', cb: (consumerTag: string, err: any) => void): this; /** An undeliverable message was published with the "immediate" flag set, or * an unroutable message published with the "mandatory" flag set. The reply * code and text provide information about the reason that the message was * undeliverable. * {@label BASIC_RETURN} */ on(name: 'basic.return', cb: (msg: ReturnedMessage) => void): this; /** The channel was closed, because you closed it, or due to some error */ on(name: 'close', cb: () => void): this; /** Certain errors are emitted if the channel was acquired with * emitErrorsFromChannel=true, otherwise these are emitted from the * Connection object. */ on(name: 'error', cb: (err: any) => void): this; } /** * @see {@link Connection#acquire | Connection#acquire()} * @see {@link Connection#createConsumer | Connection#createConsumer()} * @see {@link Connection#createPublisher | Connection#createPublisher()} * @see {@link Connection#createRPCClient | Connection#createRPCClient()} * * A raw Channel can be acquired from your Connection, but please consider * using a higher level abstraction like a {@link Consumer} or * {@link Publisher} for most cases. * * AMQP is a multi-channelled protocol. Channels provide a way to multiplex a * heavyweight TCP/IP connection into several light weight connections. This * makes the protocol more “firewall friendly” since port usage is predictable. * It also means that traffic shaping and other network QoS features can be * easily employed. Channels are independent of each other and can perform * different functions simultaneously with other channels, the available * bandwidth being shared between the concurrent activities. * * @example * ``` * const rabbit = new Connection() * * // Will wait for the connection to establish and then create a Channel * const ch = await rabbit.acquire() * * // Channels can emit some events too (see documentation) * ch.on('close', () => { * console.log('channel was closed') * }) * * // Create a queue for the duration of this connection * await ch.queueDeclare({queue: 'my-queue'}) * * // Enable publisher acknowledgements * await ch.confirmSelect() * * const data = {title: 'just some object'} * * // Resolves when the data has been flushed through the socket or if * // ch.confirmSelect() was called: will wait for an acknowledgement * await ch.basicPublish({routingKey: 'my-queue'}, data) * * const msg = ch.basicGet('my-queue') * console.log(msg) * * await ch.queueDelete('my-queue') * * // It's your responsibility to close any acquired channels * await ch.close() * ``` */ export declare class Channel extends EventEmitter { readonly id: number; /** False if the channel is closed */ active: boolean; /** Close the channel */ close(): Promise<void>; /** * This method publishes a message to a specific exchange. The message will * be routed to queues as defined by the exchange configuration. * * If the body is a string then it will be serialized with * contentType='text/plain'. If body is an object then it will be serialized * with contentType='application/json'. Buffer objects are unchanged. * * If publisher-confirms are enabled, then this will resolve when the * acknowledgement is received. Otherwise this will resolve after writing to * the TCP socket, which is usually immediate. Note that if you keep * publishing while the connection is blocked (see * {@link Connection#on:BLOCKED | Connection#on('connection.blocked')}) then * the TCP socket buffer will eventually fill and this method will no longer * resolve immediately. */ basicPublish(envelope: Envelope, body: MessageBody): Promise<void>; /** Send directly to a queue. Same as `basicPublish({routingKey: queue}, body)` */ basicPublish(queue: string, body: MessageBody): Promise<void>; /** @ignore */ basicPublish(envelope: string | Envelope, body: MessageBody): Promise<void>; /** * This is a low-level method; consider using {@link Connection#createConsumer | Connection#createConsumer()} instead. * * Begin consuming messages from a queue. Consumers last as long as the * channel they were declared on, or until the client cancels them. The * callback `cb(msg)` is called for each incoming message. You must call * {@link Channel#basicAck} to complete the delivery, usually after you've * finished some task. */ basicConsume(params: MethodParams[Cmd.BasicConsume], cb: (msg: AsyncMessage) => void): Promise<MethodParams[Cmd.BasicConsumeOK]>; /** Stop a consumer. */ basicCancel(consumerTag: string): Promise<MethodParams[Cmd.BasicCancelOK]>; basicCancel(params: MethodParams[Cmd.BasicCancel]): Promise<MethodParams[Cmd.BasicCancelOK]>; /** @ignore */ basicCancel(params: string | MethodParams[Cmd.BasicCancel]): Promise<MethodParams[Cmd.BasicCancelOK]>; /** * This method sets the channel to use publisher acknowledgements. * https://www.rabbitmq.com/confirms.html#publisher-confirms */ confirmSelect(): Promise<void>; /** * Don't use this unless you know what you're doing. This method is provided * for the sake of completeness, but you should use `confirmSelect()` instead. * * Sets the channel to use standard transactions. The client must use this * method at least once on a channel before using the Commit or Rollback * methods. Mutually exclusive with confirm mode. */ txSelect(): Promise<void>; /** Declare queue, create if needed. If `queue` empty or undefined then a * random queue name is generated (see the return value). */ queueDeclare(params: MethodParams[Cmd.QueueDeclare]): Promise<MethodParams[Cmd.QueueDeclareOK]>; queueDeclare(queue?: string): Promise<MethodParams[Cmd.QueueDeclareOK]>; /** @ignore */ queueDeclare(params?: string | MethodParams[Cmd.QueueDeclare]): Promise<MethodParams[Cmd.QueueDeclareOK]>; /** Acknowledge one or more messages. */ basicAck(params: MethodParams[Cmd.BasicAck]): void; /** Request a single message from a queue. Useful for testing. */ basicGet(params: MethodParams[Cmd.BasicGet]): Promise<undefined | SyncMessage>; basicGet(queue?: string): Promise<undefined | SyncMessage>; /** @ignore */ basicGet(params?: string | MethodParams[Cmd.BasicGet]): Promise<undefined | SyncMessage>; /** Reject one or more incoming messages. */ basicNack(params: MethodParams[Cmd.BasicNack]): void; /** Specify quality of service. */ basicQos(params: MethodParams[Cmd.BasicQos]): Promise<void>; /** * This method asks the server to redeliver all unacknowledged messages on a * specified channel. Zero or more messages may be redelivered. */ basicRecover(params: MethodParams[Cmd.BasicRecover]): Promise<void>; /** Bind exchange to an exchange. */ exchangeBind(params: MethodParams[Cmd.ExchangeBind]): Promise<void>; /** Verify exchange exists, create if needed. */ exchangeDeclare(params: MethodParams[Cmd.ExchangeDeclare]): Promise<void>; /** Delete an exchange. */ exchangeDelete(params: MethodParams[Cmd.ExchangeDelete]): Promise<void>; /** Unbind an exchange from an exchange. */ exchangeUnbind(params: MethodParams[Cmd.ExchangeUnbind]): Promise<void>; /** * This method binds a queue to an exchange. Until a queue is bound it will * not receive any messages. In a classic messaging model, store-and-forward * queues are bound to a direct exchange and subscription queues are bound to * a topic exchange. */ queueBind(params: MethodParams[Cmd.QueueBind]): Promise<void>; /** This method deletes a queue. When a queue is deleted any pending messages * are sent to a dead-letter queue if this is defined in the server * configuration, and all consumers on the queue are cancelled. If `queue` is * empty or undefined then the last declared queue on the channel is used. */ queueDelete(params: MethodParams[Cmd.QueueDelete]): Promise<MethodParams[Cmd.QueueDeleteOK]>; queueDelete(queue?: string): Promise<MethodParams[Cmd.QueueDeleteOK]>; /** @ignore */ queueDelete(params?: string | MethodParams[Cmd.QueueDelete]): Promise<MethodParams[Cmd.QueueDeleteOK]>; /** Remove all messages from a queue which are not awaiting acknowledgment. * If `queue` is empty or undefined then the last declared queue on the * channel is used. */ queuePurge(queue?: string): Promise<MethodParams[Cmd.QueuePurgeOK]>; queuePurge(params: MethodParams[Cmd.QueuePurge]): Promise<MethodParams[Cmd.QueuePurgeOK]>; /** @ignore */ queuePurge(params?: string | MethodParams[Cmd.QueuePurge]): Promise<MethodParams[Cmd.QueuePurgeOK]>; /** Unbind a queue from an exchange. */ queueUnbind(params: MethodParams[Cmd.QueueUnbind]): Promise<void>; /** * This method commits all message publications and acknowledgments performed * in the current transaction. A new transaction starts immediately after a * commit. */ txCommit(): Promise<void>; /** * This method abandons all message publications and acknowledgments * performed in the current transaction. A new transaction starts immediately * after a rollback. Note that unacked messages will not be automatically * redelivered by rollback; if that is required an explicit recover call * should be issued. */ txRollback(): Promise<void>; }