Skip to content

Instantly share code, notes, and snippets.

@alexandrebl
Last active February 5, 2022 14:40
Show Gist options
  • Select an option

  • Save alexandrebl/e45d142c44cfaf8eb7b7bc3a6fb3f4b3 to your computer and use it in GitHub Desktop.

Select an option

Save alexandrebl/e45d142c44cfaf8eb7b7bc3a6fb3f4b3 to your computer and use it in GitHub Desktop.
NATS.io.Subject.Based.Messaging.Subscriber
using NATS.Client;
using System;
namespace NatsSub1
{
internal class Program
{
static void Main(string[] args)
{
// Define variáveis
var subscrption = GetSubscription(args);
// Obtem a conexão
var connection = GetConnection();
// Assinatura
var sAsync = connection.SubscribeAsync(subscrption);
// Processamento do evento a cada mensagem recebida
sAsync.MessageHandler += (sender, args) =>
{
Console.WriteLine($"{DateTime.Now:F} - Received: {args.Message}");
};
// Inicia a leitura da assinatura
sAsync.Start();
// Aguarda uma tecla ser pressionada
Console.ReadLine();
// Desconectando
sAsync.Unsubscribe();
connection.Drain();
connection.Close();
}
/// <summary>
/// Obtem a assinatura
/// </summary>
/// <param name="args">argumentos de linha de comando</param>
/// <returns>assinatura</returns>
public static string GetSubscription(String[] args) => args[0];
/// <summary>
/// Obtem uma nova conexão
/// </summary>
/// <returns>nova conexão com NATs.io</returns>
public static IConnection GetConnection()
{
var cf = new ConnectionFactory();
var c = cf.CreateConnection();
return c;
}
}
}
@alexandrebl
Copy link
Copy Markdown
Author

alexandrebl commented Feb 5, 2022

natssub "subject"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment