UNPKG

rabbitmq-client

Version:
110 lines (109 loc) 4.19 kB
import type { AsyncMessage, MethodParams, Envelope, Cmd } from './codec'; export interface RPCProps { /** Enable publish-confirm mode. See {@link Channel.confirmSelect} */ confirm?: boolean; /** Any exchange-exchange bindings to be declared before the consumer and * whenever the connection is reset. */ exchangeBindings?: Array<MethodParams[Cmd.ExchangeBind]>; /** Any exchanges to be declared before the consumer and whenever the * connection is reset */ exchanges?: Array<MethodParams[Cmd.ExchangeDeclare]>; /** Retries are disabled by default. * Increase this number to retry when a request fails due to timeout or * connection loss. The Connection options acquireTimeout, retryLow, and * retryHigh will affect time between retries. * @default 1 */ maxAttempts?: number; /** Any queue-exchange bindings to be declared before the consumer and * whenever the connection is reset. */ queueBindings?: Array<MethodParams[Cmd.QueueBind]>; /** Define any queues to be declared before the first publish and whenever * the connection is reset. Same as {@link Channel.queueDeclare} */ queues?: Array<MethodParams[Cmd.QueueDeclare]>; /** Max time to wait for a response, in milliseconds. * Must be > 0. Note that the acquireTimeout will also affect requests. * @default 30_000 * */ timeout?: number; } /** * @see {@link Connection#createRPCClient | Connection#createRPCClient()} * @see {@link RPCProps} * @see {@link https://www.rabbitmq.com/direct-reply-to.html} * * This will create a single "client" `Channel` on which you may publish * messages and listen for direct responses. This can allow, for example, two * micro-services to communicate with each other using RabbitMQ as the * middleman instead of directly via HTTP. * * If you're using the createConsumer() helper, then you can reply to RPC * requests simply by using the `reply()` argument of * the {@link ConsumerHandler}. * * Also, since this wraps a Channel, this must be closed before closing the * Connection: `RPCClient.close()` * * @example * ``` * // rpc-client.js * const rabbit = new Connection() * * const rpcClient = rabbit.createRPCClient({confirm: true}) * * const res = await rpcClient.send('my-rpc-queue', 'ping') * console.log('response:', res.body) // pong * * await rpcClient.close() * await rabbit.close() * ``` * * ``` * // rpc-server.js * const rabbit = new Connection() * * const rpcServer = rabbit.createConsumer({ * queue: 'my-rpc-queue' * }, async (req, reply) => { * console.log('request:', req.body) * await reply('pong') * }) * * process.on('SIGINT', async () => { * await rpcServer.close() * await rabbit.close() * }) * ``` * * If you're communicating with a different rabbitmq client implementation * (maybe in a different language) then the consumer should send responses * like this: * ``` * ch.basicPublish({ * routingKey: req.replyTo, * correlationId: req.correlationId, * exchange: "" * }, responseBody) * ``` */ export declare class RPCClient { /** True while the client has not been explicitly closed */ active: boolean; /** Like {@link Channel#basicPublish}, but it resolves with a response * message, or rejects with a timeout. * Additionally, some fields are automatically set: * - {@link Envelope.replyTo} * - {@link Envelope.correlationId} * - {@link Envelope.expiration} */ send(envelope: Envelope, body: any): Promise<AsyncMessage>; /** Send directly to a queue. Same as `send({routingKey: queue}, body)` */ send(queue: string, body: any): Promise<AsyncMessage>; /** @ignore */ send(envelope: string | Envelope, body: any): Promise<AsyncMessage>; /** @deprecated Alias for {@link RPCClient#send} */ publish(envelope: string | Envelope, body: any): Promise<AsyncMessage>; /** Stop consuming messages. Close the channel once all pending message * handlers have settled. If called while the Connection is reconnecting, * then this may be delayed by {@link ConnectionOptions.acquireTimeout} */ close(): Promise<void>; }