PubSubClient 1.0.3
dotnet add package PubSubClient --version 1.0.3
NuGet\Install-Package PubSubClient -Version 1.0.3
<PackageReference Include="PubSubClient" Version="1.0.3" />
paket add PubSubClient --version 1.0.3
#r "nuget: PubSubClient, 1.0.3"
// Install PubSubClient as a Cake Addin #addin nuget:?package=PubSubClient&version=1.0.3 // Install PubSubClient as a Cake Tool #tool nuget:?package=PubSubClient&version=1.0.3
PubSubClient
PubSubClient is a flexible and powerful publish-subscribe (pub/sub) messaging library designed for .NET applications. It enables easy integration of event-driven architectures with a variety of message brokers, allowing you to publish and subscribe to messages in a decoupled manner.
Features
- Broker-Agnostic: Supports multiple message brokers such as RabbitMQ, Kafka, and others.
- Easy Integration: Designed to work seamlessly with ASP.NET Core and other .NET applications.
- Asynchronous Processing: Built-in support for asynchronous message processing.
- Scalability: Suitable for high-throughput, distributed environments.
Installation
To install PubSubClient in your project, you can use the following NuGet command for your provider:
dotnet add package PubSubClient.ActiveMQ
dotnet add package PubSubClient.Kafka
dotnet add package PubSubClient.RabbitMQ
Usage
Basic Setup
Configuration
PubSubClient expects the configuration for the broker connection in the
MessageBroker
section of the config. Example configurations provided below for each provider and links to documentation.Register PubSubClient with the desired broker in your DI Container:
using PubSubClient; using PubSubClient.RabbitMQ public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddPubSubClient<RabbitMqConsumerService, RabbitMqPublisherService>(); // Example using RabbitMQ } }
Define a Service Definition:
using PubSubClient; public class MyServiceDefinition : MicroServiceDefinitionBase { public override string ExchangeName => "MyExchange"; }
Publish a message:
public class MyPublisherService { private readonly IPublisherService _publisher; public MyPublisherService(IPublisherService publisher) { _publisher = publisher; } public async Task PublishMessageAsync() { var message = new MyMessage { MessageContent = "Hello, PubSub!" }; await _publisher.PublishAsync(message, new MyServiceDefinition()); } }
Consume messages:
using PubSubClient.Attributes; public class Consumers { [ConsumerMethod(typeof(MyServiceDefinition))] public async Task RunMyMethod(object o, MyMessage message) { await Task.FromResult(0); Console.WriteLine($"Received message: {message.MessageContent}"); } }
Note: The consumer method must a signature of (object, T) or (T). The object passed is the entire message with metadata from the broker.
Supported Message Brokers
PubSubClient supports multiple brokers. Each broker can be installed as a separate NuGet package:
RabbitMQ: A popular message broker for reliable messaging.
"MessageBroker": { "HostName": "localhost:5672", "DispatchConsumersAsync": true }
See here for documentation.
Kafka: High-throughput, distributed streaming platform.
"MessageBroker": { "BootstrapServers": "localhost:9092", "saslUsername": "admin", "saslPassword": "admin123", "SecurityProtocol": 2, "SaslMechanism": 1, "AutoOffsetReset": 1 }
See here for documentation.
Apache ActiveMQ: Open-source, multi-protocol distributed messaging platform.
"MessageBroker": { "MqServerName": "activemq:tcp://localhost:61616", "MqUserName": "artemis", "MqPassword": "artemis", }
Note: These are the only configuration options available for ActiveMQ
Adding Custom Brokers
You can also implement your own custom consumer and publisher by creating a class that implements the IConsumerService
and IPublisherService
interfaces.
public class MyCustomConsumerService : IConsumerService
{
// Implementation goes here
}
public class MyCustomPublisherService : IPublisherService
{
// Implementation goes here
}
License
PubSubClient is licensed under the GPL License. See the LICENSE for more information.
Contributing
Contributions are always welcome! Feel free to open an issue or submit a pull request.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Hosting (>= 8.0.0)
-
net8.0
- Microsoft.Extensions.Hosting (>= 8.0.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on PubSubClient:
Package | Downloads |
---|---|
PubSubClient.ActiveMQ
Consumer and Publisher services for adding Apache ActiveMQ Message Broker services to applications |
|
PubSubClient.RabbitMQ
Consumer and Publisher services for adding RabbitMQ Message Broker services to applications |
|
PubSubClient.Kafka
Consumer and Publisher services for adding Kafka Message Broker services to applications |
GitHub repositories
This package is not used by any popular GitHub repositories.