EventStore Subscribing to a stream for a category

You need to check if projections are running, since this feature is disabled by default at the time of writing this post since the Projections feature is still in beta.

There is a special type projection called category projection that is what you probably need. If you have streams that are named following the name-id pattern, for example product-123, product-234, you can subscribe to $ce-product stream to receive all events that are saved to these streams.


In your projections you can use fromCategory(). EventStore categorises each stream by the name of the stream up to the first '-'. So your 'order-123' and 'order-456' streams are both in the 'order' category.

So you can do something like:

fromCategory('order')
  .whenAny(function(s,e) {
    linkTo('orders',e);
  });

This will project all order related events from all order aggregates into a single 'orders' stream that you can subscribe.

You'll need to ensure that the category projections are running. (It's been a while since I've used EventStore, it was back when projections were in beta and weren't enabled by default, not sure if things are the same in the latest versions)


From The cost of creating a stream article:

Generally when people are wanting only a few streams its because they want to read things out in a certain way for a particular type of reader. This can be done in other ways. Internally the Event Store is essentialy a topic based pub/sub. What you can do is repartition your streams utilizing projections to help provide for a specific reader. As an example let's say that a reader was interested in all the InventoryItemCreated and InventoryItemDeactivated events but was not interested in all the other events in the system. Supporting this stream when we have the events in many millions of streams can still be done.

To do this we will create a projection to reindex the streams.

So the idea is that you could create two projections to emit events to some products and orders streams.

In the same article the system bytype projection is mentioned, which creates a stream for each event type with the name $et-{typename}. This could also prove useful in your case.

For instance, if you are only interested in observing the creation of the aggregates, you could subscribe to the corresponding streams. Assuming that you have some productCreated and orderCreated events, you would simply subscribe to the $et-productCreated and $et-orderCreated events. Upon receiving events from these streams, you could also subscribe to the individual streams (eg. product-553 and order-123) simply by consuming the Id from the *Created events.

(Note: projections are disabled by default and once enabled you may need to manually start the bytype projection.)