Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publisher Confirms - How to use #2

Closed
paulohsilvavieira opened this issue Sep 23, 2022 · 1 comment
Closed

Publisher Confirms - How to use #2

paulohsilvavieira opened this issue Sep 23, 2022 · 1 comment

Comments

@paulohsilvavieira
Copy link

paulohsilvavieira commented Sep 23, 2022

Hi! I am create a example with lib for study and practice, but i not found how to get the confirmation, i found only function confirmSelection(), how i get the confirmation on publisher ? I read the code and found confirm.select-ok, but dont found how to use this method

@cody-greene
Copy link
Owner

Refer to the official (server) documentation for more information on publisher confirms https://www.rabbitmq.com/confirms.html#publisher-confirms

To enable confirms, a client sends the confirm.select method. Depending on whether no-wait was set or not, the broker may respond with a confirm.select-ok. Once the confirm.select method is used on a channel, it is said to be in confirm mode.

The broker then confirms messages as it handles them by sending a basic.ack on the same channel.

You can enable publisher confirms with await channel.confirmSelect(). This library handles publisher confirms automatically, without the need for further user intervention. When you publish a message on a channel in confirm-mode, the await channel.basicPublish(...) call will resolve only when the confirmation (basic.ack) is received.

Full example:

import {Connection} from 'rabbitmq-client'
const rabbit = new Connection()
async function main() {
  const ch = await rabbit.acquire()
  
  await ch.confirmSelect()
  await ch.basicPublish({routingKey: 'foo.bar'}, 'some-example-data')
  await ch.basicPublish({routingKey: 'foo.bar'}, 'some-other-data')
  
  await ch.close()
  await rabbit.close()
}
main()

Throughout this library I have tried to document important behavior where appropriate, and provide links to the official RabbitMQ docs for further context. The relevant notes for this library can be found here:

* 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 |
* Connection.on('connection.blocked')}) then the TCP socket buffer will
* eventually fill and this method will no longer resolve immediately.

/**
* This method sets the channel to use publisher acknowledgements. The client
* can only use this method on a non-transactional channel.
* https://www.rabbitmq.com/confirms.html#publisher-confirms
*/
async confirmSelect(): Promise<void> {

Also consider using a higher level "Publisher" object for more safety. rabbit.createPublisher({confirm: true})

/**
* Create a message publisher that can recover from dropped connections.
* This will create a dedicated Channel, declare queues, declare exchanges,
* and declare bindings. If the connection is reset, then all of this setup
* will rerun on a new Channel.
*/
createPublisher(props: PublisherProps = {}): Publisher {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants