Event Store subscription service to MongoDB

1.4k Views Asked by At

I was wondering if it was possible to create a subscription service to Mongo via Get Event Store? Perhaps I have phrased this incorrectly but let me explain. I currently have a process which writes events to a Mongo Database using NEventStore. What I would like to do is have a subscription service which subscribes to a Stream in Mongo.

Not been able to find anything on the interweb about this in particular however is this possible? My question in a nutshell perhaps is can you mix and match the two together or in order to do this I must write my events to eventstore as opposed to Mongo? Perhaps, I am going about this wrong and there is an alternative?

I can see my event being written however it fails to trigger EventAppeared. All of this is being done locally on my machine for now.

I have tried creating a stripped down app which does this:

  1. Create a subscription using the following

        using (var connection = EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, 1113)))
        {
            connection.SubscribeToStreamAsync(@"mongodb://localhost:27017/Test", false, EventAppeared, SubscriptionDropped);
    
            var repository = new NEventStoreRepository();
            repository.Write(new SomethingHasHappened("Hello"));
    
            Console.ReadLine();
        }
    
    private static void SubscriptionDropped(EventStoreSubscription arg1, SubscriptionDropReason arg2, Exception arg3)
    {
    }
    
    private static void EventAppeared(EventStoreSubscription arg1, ResolvedEvent arg2)
    {
    }
    
  2. I write an event to my mongo database via NEventStore

    public void Write(object @event)
    {
        var id = Guid.NewGuid();
    
        using (var scope = new TransactionScope())
        {
            using (var store = WireupEventStore())
            {
                using (var stream = store.OpenStream(id.ToString(), 0, int.MaxValue))
                {
                    stream.Add(new EventMessage { Body = @event });
                    stream.CommitChanges(Guid.NewGuid());
                    scope.Complete();
                }
            }
        }
    
        Console.ReadKey();
    }
    
    private static IStoreEvents WireupEventStore()
    {
        return Wireup
            .Init()
            .LogToOutputWindow()
            .UsingMongoPersistence("NEventStore.MongoDB", new DocumentObjectSerializer())
            .InitializeStorageEngine()
            .UsingJsonSerialization()
            .Build();
    }
    
1

There are 1 best solutions below

3
tomliversidge On BEST ANSWER

The normal flow of events for this would be as follows:

(given everything is installed and running...)

  1. Register a subscriber to a stream in GetEventStore in your application code
  2. Save events to the stream
  3. The events appear in your subscriber

I think you are either confusing the flow of things or trying to do something totally unsupported (like having MongoDb subscriber to GetEventStore). What I think your code is doing is:

  1. Setting up NEventStore to save to MongoDb
  2. Subscribing to a stream in GetEventStore called "mongodb://localhost:27017/Test"
  3. Saving an event to MongoDb

As far as I can see, you're never saving any events to GetEventStore, hence why nothing ever appears in the EventAppeared method. You're saving to MongoDb.

[UPDATE]

I want to subscribe to a Mongodb stream and populate GetEventStore which I believe isn't possible from what I gather from your answer.

MongoDb doesn't have streams, it has collections - it is a document database. Streams are a concept in GetEventStore. However, it looks like NEventStore allows you to hook up a message dispatcher which presumably means you can register handlers to listen to events. In these handlers, you can then save to GetEventStore.