Use the Standard API (Flowing vs Non-flowing modes)
masterThe Standard API allows manual management of callbacks and events. You can operate in two modes:
- Flowing mode: Messages flow continuously via an infinite loop in the event loop. This is triggered by calling
consumer.consume()without a callback (or with only a callback). - Non-flowing mode: You manually request messages. This is triggered by calling
consumer.consume(number, cb)wherenumberis the amount of messages to fetch.
Important: consumer.consume() uses background threads. The number of threads is limited by UV_THREADPOOL_SIZE (default 4). If using multiple consumers, increase UV_THREADPOOL_SIZE or use the (number, cb) variant to avoid blocking the application.
// Flowing mode example
consumer.connect();
consumer
.on('ready', () => {
consumer.subscribe(['librdtesting-01']);
consumer.consume();
})
.on('data', (data) => {
console.log(data.value.toString());
});
// Non-flowing mode example
consumer.connect();
consumer
.on('ready', () => {
consumer.subscribe(['librdtesting-01']);
setInterval(() => {
consumer.consume(1);
}, 1000);
})
.on('data', (data) => {
console.log(data.value.toString());
});