EventStore.Client.Grpc.PersistentSubscriptions - example

Hello Community!

I need help with using EventStore.Client.Grpc.PersistentSubscriptions. Can you provide sample code how to connect to subscription and how to specify method which will be called every time the event came.

Thank you!

1 Like

Hi there,

I found that a great way to start things off is by checking out the eventstore client tests on github:

Otherwise I have here a simple sample:

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using EventStore.Client;

namespace ConsoleApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var credentials = new UserCredentials("admin", "changeit");
            
            var settings = new EventStoreClientSettings
            {
                CreateHttpMessageHandler = () =>
                    new HttpClientHandler {
                        ServerCertificateCustomValidationCallback =
                            (message, certificate2, x509Chain, sslPolicyErrors) => true // ignore https
                    },
                ConnectivitySettings = {Address = new Uri("https://xxx.xxx.xxx.xxx:2113")},
                ConnectionName = "Test20.06",
                DefaultCredentials = credentials
            };
            
            var client = new EventStorePersistentSubscriptionsClient(settings);

            await client.CreateAsync("newstream", "newgroup", new PersistentSubscriptionSettings(), credentials);
            
            await SubscribeAsync(client);

            Console.ReadLine();
        }

        private static Task<PersistentSubscription> SubscribeAsync(EventStorePersistentSubscriptionsClient client)
        {
            return client.SubscribeAsync("newstream", "newgroup",
                (subscription, evt, retryCount, cancelToken) =>
                {
                    Console.WriteLine("Received: " + Encoding.UTF8.GetString(evt.Event.Data.Span));

                    return Task.CompletedTask;
                });
        }
    }
}

-Stephen

1 Like

This should be described in the documentation or?

I don’t believe the documentation for the new GRPC clients are out yet. I think the evenstore team is working hard to deliver that. I would love to get my hands on it too!

-Stephen

Looks like the “draft” doc on this is out!

https://developers.eventstore.com/clients/dotnet/generated/v20.6.0/subscribing-to-streams/basics.html#subscribing-to-a-stream

1 Like