SubscribeAsync & UnsubscribeAsync

Hi all,

Did anyone played with the methods above from the EventStoreConnection or can someone please describe what would be scenario for using them.

My aim would be to create read model soon as event is stored, within same process and try to store it somewhere (document database or relation one).

If storing of the read model fails I would like to know from where to replay events in order to rebuild it.(Is this to complicated, any other simpler way without having to query eventstore projections?)

One question what puzzles me: Does SubscribeAsync lives only within open connection r it is stored in the store forever until we Unsubscribe from it, what means we have to maintain subscriptions.

From the client code I can see that the subscribe request is sent to the store, but don’t know where to see how many subscriptions are registered in the store if they are persistent within store.

Below code does not work and if someone know why or what I’m doing wrong please let me know.

public void StoreAggregateChanges(T aggregateInstance) where T : IAggregate

{

string streamId = GetStreamId(aggregateInstance.GetType(), aggregateInstance.Id);

var tempSubscribe = new StreamSubsciption(streamId);

using (EventStoreConnection storeConnection = EventStoreConnection.Create())

{

storeConnection.Connect(_endpoint);

storeConnection.SubscribeAsync(tempSubscribe.StreamName, tempSubscribe.EventRecorded,

tempSubscribe.SubscriptionFailed);

storeConnection.AppendToStream(streamId, ExpectedVersion.Any,

aggregateInstance.GetUnocommittedEventsForEventStore());

aggregateInstance.ClearUncommittedEvents();

** storeConnection.UnsubscribeAsync(streamId); with this line SubscriptionFailed method is called, do I need this line? when I comment out this line EventRecorded work fine but will I end up with long living subscriptions what I don’t need**

}

}

internal class StreamSubsciption

{

public readonly string StreamName;

public StreamSubsciption(string streamName)

{

Console.WriteLine(“Stream name:{0}”,streamName);

StreamName = streamName;

}

public void EventRecorded(RecordedEvent recordedEvent, Position position)

{

Console.Write(“ThredId:{0}”,System.Threading.Thread.CurrentThread.ManagedThreadId);

Console.WriteLine(“recorded event id:{0},Position Commit:{1},Prepare:{2}”,recordedEvent.EventId,position.CommitPosition,position.PreparePosition);

}

public void SubscriptionFailed()

{

Console.WriteLine(“Failed”);

//nooonoo

}

}

Cheers Tom

I believe I’ve found an answer …

We are subscribing into in memory bus within store and after connection get closed it does unsubscribe for us***.***

Therefore subscription lives while connection is open towards EventStore and we do not have to manage it after connection is closed.

What would be then purpose of the unsubscribe method?

It would be nice to know as well what is reason for the subscription to fail …

Like instead of

public Task SubscribeAsync(string stream, Action<RecordedEvent, Position> eventAppeared, Action subscriptionDropped)

having

public Task SubscribeAsync(string stream, Action<RecordedEvent, Position> eventAppeared, Action subscriptionDropped)

where string would contain the reason why …

for my case would be look at the SingleNode.exe ClientService to get an answer :wink:

Cheers

The dropped happens if you don’t keep up with the subscription data.

If the queue becomes too large in the server it will kill your subscription.