DEV Community

Cover image for Brighter V10: Using PostgreSQL as a Lightweight Messaging Gateway
Rafael Andrade
Rafael Andrade

Posted on

Brighter V10: Using PostgreSQL as a Lightweight Messaging Gateway

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

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;
}
Enter fullscreen mode Exit fullscreen mode
  • 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Configuring Brighter with Postgres

1. Connection Setup

Define Postgres connection details:

var config = new RelationalDatabaseConfiguration(
  "<connection-string>", 
   queueStoreTable: "QueueData" // Auto-created if missing
);
Enter fullscreen mode Exit fullscreen mode

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));
})
Enter fullscreen mode Exit fullscreen mode

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();
})
Enter fullscreen mode Exit fullscreen mode

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)