static void Main(string[] args)
        {
            Console.WriteLine("Starting subsriber app...");

            var natsConnectionOptions = StanOptions.GetDefaultOptions();

            natsConnectionOptions.NatsURL = "nats://localhost:4222";
            natsConnectionOptions.ServerHeartbeatTimeoutCallback = () =>
            {
                Console.WriteLine("Hurrr... connection problem!");
            };
            natsConnectionOptions.ServerHeartbeatTimeoutMillis = 10000;

            var stanConnection = new StanConnectionFactory().CreateConnection("test-cluster", $"subscirber-name", natsConnectionOptions);

            var options = StanSubscriptionOptions.GetDefaultOptions();


            stanConnection.Subscribe($"some-channel", options, (sender, e) =>
            {
                Console.WriteLine("Message received...");
            });

            Console.WriteLine("Started... Hit any key to quit.");
            Console.ReadKey();
        }
Example #2
0
        private async Task Subscriber(string dataCentre, CancellationToken token)
        {
            var subscriberOptions = StanOptions.GetDefaultOptions();

            subscriberOptions.NatsURL = _natsClusterNodes[dataCentre];
            var loopCount = 0;

            using var stanConnection = new StanConnectionFactory().CreateConnection(ClusterId, $"{Environment.MachineName.ToLower()}-{dataCentre}-receiving", subscriberOptions);
            using var subscribe      = stanConnection.Subscribe("foo", CreateStanSubscriptionOptions(dataCentre, subscriberOptions), (obj, context) =>
            {
                Console.WriteLine(
                    $"{dataCentre}: received '{System.Text.Encoding.UTF8.GetString(context.Message.Data)}'. Redelivered: {context.Message.Redelivered}. Sequence: {context.Message.Sequence}");
                Console.WriteLine($"loopCount: {Interlocked.Increment(ref loopCount)}");
            });

            await Task.Delay(1000000, token);

            subscribe.Unsubscribe();
            Console.WriteLine("Exiting subscription");
        }
Example #3
0
        static void Main()
        {
            string      serverURL = "nats://localhost:4222", clusterID = "test-cluster", clientID = "client_sub", subject = "foo_subject", queue = "foo_queue", durable = "foo_durable";
            StanOptions cOpts = StanOptions.GetDefaultOptions();

            cOpts.NatsURL = serverURL;
            using (var c = new StanConnectionFactory().CreateConnection(clusterID, clientID, cOpts))
            {
                Console.WriteLine("nats: connected serverURL='{0}', clusterID='{1}', clientID='{2}'", serverURL, clusterID, clientID);
                StanSubscriptionOptions sOpts = StanSubscriptionOptions.GetDefaultOptions();
                sOpts.DurableName = durable;
                sOpts.MaxInflight = 1;
                using (var s = c.Subscribe(subject, queue, sOpts, (sender, args) =>
                {
                    Console.WriteLine("Received seq # {0}: {1}", args.Message.Sequence, System.Text.Encoding.UTF8.GetString(args.Message.Data));
                }))
                {
                    Console.WriteLine("nats: subscribed subject='{0}', queue='{1}', durable='{2}'", subject, queue, durable);
                    Console.WriteLine("program: press <enter> to exit...");
                    Console.ReadLine();
                }
            }
        }