PostgreSQL Notify and Listen support using reactive spring boot

This project demonstrates how to implement PostgreSQL's NOTIFY/LISTEN feature using Spring Boot and R2DBC. It showcases real-time notifications between database and application using PostgreSQL's pub/sub capabilities.


Features

  • Reactive PostgreSQL connectivity using R2DBC

  • Implementation of PostgreSQL NOTIFY/LISTEN mechanism

  • Real-time event notifications

  • Reactive endpoints for publishing and receiving notifications


Prerequisites

  • JDK 21

  • Maven

  • Docker and Docker Compose

  • PostgreSQL (or use provided Docker Compose)


Getting Started

  1. Start PostgreSQL using Docker Compose:

  2. Build the application:

  3. Run the application:


Architecture


Usage

  1. To publish a notification:

  2. To subscribe to notifications:

    • Connect to WebSocket endpoint: ws://localhost:8080/notifications

    • Or use Server-Sent Events endpoint: http://localhost:8080/notifications/sse


Configuration

Key configuration properties in application.properties:


How It Works

  1. The application establishes a connection to PostgreSQL using R2DBC

  2. It sets up LISTEN on specified channels

  3. When a notification is published:

    • Application executes NOTIFY command

    • PostgreSQL broadcasts to all listeners

    • Listeners receive and process notifications

  4. Connected clients receive notifications via WebSocket/SSE


Contributing

Feel free to submit issues and enhancement requests!

Last updated

Was this helpful?