Asynchronous subscription handler functions

Does the Node.js client provide support for asynchronous subscription handler functions to be executed sequentially?

The use case is to create a (single) subscription for maintaining a persistent Read Model that requires sequential event processing. The event handler loads the current data, applies a modification and persists the updated data together with a cursor of the processed revision. As this is an asynchronous process, there needs to be a mechanism that queues the handler function executions. Otherwise, the handler might be executed concurrently for multiple events.

So far, I have solved this with a simple (in-memory) queuing mechanism that wraps the handler function (think chained promises). Also, I am aware that there are other possible approaches. One could put a persistent queue in between EventStoreDB and the actual subscriber or shift the responsibility of queuing to the data storage if possible.

I was just wondering if this is something that is already built into the client or might be added at some point.

I’d say that’s how NodeJS streaming works out of the box, as we don’t add much to it.

However, the subscription is also an async iterator. If you use it as an iterator, it should allow you to properly handle events one by one.

1 Like

Nice, I will try to use it as async iterator then. Thanks for the hint!