The program run fines when I have the following configuration:
let events = eventstoreClient.readStream(
streamName,
{
fromRevision: END,
direction: BACKWARDS,
maxCount: 20
}
);
However, the program stucks(i.e. could not read the streams/events further) when maxCount is set to 100 or 1000 or maxCount option is not supplied!
readStream returns a readable stream which will buffer the events internally until they exceed the highWaterMark threshold. The data will sit in the internal queue until it is consumed, and the stream will temporarily stop reading events from the server until the data currently buffered has been consumed.
When you request 20 events they fit within internal buffer, so after 20 events the stream closes and NodeJS is happy to stop execution. However, if you request 100 or 1000 events, once the internal buffer is full, the stream won't be closed as there are still events to be read from the server, and it will wait for you to consume those events.
You need to pull the events from the buffer to allow the streaming read to finish. This can be done in a number of ways, for example:
Iterate through the steam:
for await (const { event } of events) {
console.log(event);
}
or add an event listener:
events.on("data", (event) => {
console.log(event);
});
You can read more about buffering here: https://nodejs.org/api/stream.html#buffering