Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EventStore Subscribing to a stream for a category

I have started creating a test application in .Net, that uses EventStore by Greg Young as the backing store for CQRS/ES.

In order to make it easy to load up a full aggregate, I save to a stream with a name of "agg-123". For example for a product aggregate with an id of 553, there would be a stream named "product-553". And then same for an "Order" aggregate, the stream would be named "order-123".

From the rehydrating and saving of events this works well.

I am now attempting to create a listener that will listen to certain streams to then populate a query database. The subscribe methods that I see can only seem to subscribe to "order-123", or "all". I can't see how I would subscribe to "product-" or "order-", or both.

I imagine that either

  • I have missed the point with stream names, and have named them wrong
  • Have missed a way to choose it, something like "product-*"
  • It is expected to subscribe to "all" and filter out what you aren't interested in, although this gives the problem that it also sends all of the "stats" events

Anyone any advice?

like image 703
eyeballpaul Avatar asked Jan 12 '16 17:01

eyeballpaul


2 Answers

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)

like image 186
Matt Avatar answered Oct 07 '22 14:10

Matt


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.)

like image 36
jnovo Avatar answered Oct 07 '22 16:10

jnovo