Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Source
All the source code for this demo is available on GitHub. Please take a look to follow along as the rest of this post is describing how it works. Although I’ve added this to as a new repo on GitHub, It is also available in the EventStore.Samples.DotNet repo on GitHub along with other subscription model examples.Server
If you don’t have it already, download a copy of Event Store and run EventStore.ClusterNode.exe with default arguments. This will start the Event Store server under port 1113. The web UI will be accessible via http://localhost:2113Create Subscription
First you need to create a subscription to a stream. You will also provide a group name which you will also use when connecting to the subscription. In my demo I’ve included code to create the subscription. This is simply because I don’t want anyone running the demo to have to create it manually. The documentation discourages it from being created in your general application code.Normally the creating of the subscription group is not done in your general executable code. Instead it is normally done as a step during an install or as an admin task when setting things up. You should assume the subscription exists in your code.The web UI has a pretty nice interface for viewing your subscriptions as well as creating and updating them.

Connecting to Persistent Subscription
When connecting to a persistent subscription, you must specify the stream and the group in which you use to create the subscription. There are two other optional parameters which I’ve found to be important: bufferSize and autoAck bufferSize: The number of in-flight messages this the server will send to the client. autoAck: The client API will automatically acknowledge after the EventAppeared method returns. If an exception is thrown in the EventAppeared method, the subscription will drop your subscription.Dropped Subscription
I found it helpful to have the subscription reconnect when it is dropped. Another case where the subscription can be dropped is if changes to the subscription are made from another client or the web UI.EventAppeared
Method is invoked once the subscription receives an event. As you can assume the ResolvedEvent contains the event and the eventStoragePersistentSubscriptionBase allows you to acknowledge or not acknowledge (fail) a ResolvedEvent or a array of ResolvedEvents. If you have autoAck then you do not need to acknowledge.Running Demo
To run the demo, simply build the solution to generate the exe’s. Run multiple instances of PersistentSubscription.exe Multiple instances will allow you to see how each will connect to the subscription and only receive one of the events that is written to the stream.



Pingback: The Morning Brew - Chris Alcock » The Morning Brew #2037
Pingback: Background Tasks in .NET - CodeOpinion
But then how do I run a catch-up/replay for when I add new projections, etc?
You would simply create a new group on an existing stream. The new group would have a “current” position of 0. Once you connect to that stream under the new group, you would start processing from the first event.