In previous articles, I covered Brighter V10 RC1. One notable addition is support for Postgres as a messaging gateway, ideal for small applications or prototypes due to its simplicity and lightweight setup.
Requirement
- .NET 8 or superior
- A .NET project with these NuGet packages
- Paramore.Brighter.MessagingGateway.Postgres: Enables Postgres integration for message queuing.
- Paramore.Brighter.ServiceActivator.Extensions.DependencyInjection: Enable register Brighter with Microsoft DI.
- Paramore.Brighter.ServiceActivator.Extensions.Hosting: Hosts Brighter as a background service.
- Serilog.AspNetCore: For structured logging (optional but recommended).
Brighter Recap
Before continuing about Postgres configuration, let's recap what we already know about Brighter.
Request (Command/Event)
Define messages using IRequest
:
public class Greeting() : Event(Guid.NewGuid())
{
public string Name { get; set; } = string.Empty;
}
- Commands: Single-recipient operations (e.g.,
SendEmail
). - Events: Broadcast notifications (e.g.,
OrderShipped
).
Message Mapper (Optional)
Translates between Brighter messages and your app objects, by default Brighter will use JSON serialize.
Request Handler
Processes incoming messages:
public class GreetingHandler(ILogger<GreetingHandler> logger) : RequestHandler<Greeting>
{
public override Greeting Handle(Greeting command)
{
logger.LogInformation("Hello {Name}", command.Name);
return base.Handle(command);
}
}
Configuring Brighter with Postgres
1. Connection Setup
Define Postgres connection details:
var config = new RelationalDatabaseConfiguration(
"<connection-string>",
queueStoreTable: "QueueData" // Auto-created if missing
);
Note: If the queue table doesn't exist, Brighter will create it automatically
2. Postgres Subscription
Subscribe to a topic:
.AddServiceActivator(opt =>
{
opt.Subscriptions =
[
new PostgresSubscription<Greeting>(
subscriptionName: new SubscriptionName("greeting.subscription"),
channelName: new ChannelName("greeting.topic"),
makeChannels: OnMissingChannel.Create,
messagePumpType: MessagePumpType.Reactor,
timeOut: TimeSpan.FromSeconds(10)
),
];
opt.DefaultChannelFactory = new PostgresChannelFactory(new PostgresMessagingGatewayConnection(config));
})
3. Postgres Producer Configuration
Publish events to a topic:
.UseExternalBus(opt =>
{
opt.ProducerRegistry = new PostgresMessagingGatewayConnection(config, [
new PostgresPublication<Greeting>
{
Topic = new RoutingKey("greeting.topic"),
MakeChannels = OnMissingChannel.Create
}]).Create();
})
Conclusion
Integrating Brighter with Postgres simplifies building scalable, message-driven systems for lightweight use cases. By leveraging V10’s improved defaults and Postgres’ reliability, developers can focus on business logic without the overhead of complex infrastructure.
Key Benefits
- Transactional consistency: Leverage Postgres' ACID guarantees
- Simplified infrastructure: Single database for app data + messaging
- Rapid prototyping: Eliminate dedicated message broker dependencies
Note: For production workloads requiring high throughput, consider dedicated brokers like Kafka or RabbitMQ.
For a complete working example, refer to the GitHub sample repository.
Top comments (0)