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.
- Reactive PostgreSQL connectivity using R2DBC
- Implementation of PostgreSQL NOTIFY/LISTEN mechanism
- Real-time event notifications
- Reactive endpoints for publishing and receiving notifications
- JDK 21
- Maven
- Docker and Docker Compose
- PostgreSQL (or use provided Docker Compose)
-
Start PostgreSQL using Docker Compose:
docker-compose up -d
-
Build the application:
./mvnw clean package
-
Run the application:
./mvnw spring-boot:run
sequenceDiagram
participant Client
participant Application
participant Listener
participant PostgreSQL
Note over Application: Application starts
Application->>PostgreSQL: Connect to database
Application->>PostgreSQL: LISTEN channel_name
Note over Client: Trigger notification
Client->>Application: POST /api/notify
Application->>PostgreSQL: NOTIFY channel_name, 'message'
PostgreSQL-->>Listener: Notification event
Listener-->>Application: Process notification
Application-->>Client: Notification processed
Note over Client: Subscribe to events
Client->>Application: WebSocket connection
Application-->>Client: Stream notifications
-
To publish a notification:
curl -X POST http://localhost:8080/api/notify \ -H "Content-Type: application/json" \ -d '{"channel": "events", "message": "Hello World!"}'
-
To subscribe to notifications:
- Connect to WebSocket endpoint:
ws://localhost:8080/notifications
- Or use Server-Sent Events endpoint:
http://localhost:8080/notifications/sse
- Connect to WebSocket endpoint:
Key configuration properties in application.properties
:
# R2DBC PostgreSQL Configuration
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/postgres
spring.r2dbc.username=postgres
spring.r2dbc.password=postgres
# Notification Channel Configuration
app.notification.channel=events
- The application establishes a connection to PostgreSQL using R2DBC
- It sets up LISTEN on specified channels
- When a notification is published:
- Application executes NOTIFY command
- PostgreSQL broadcasts to all listeners
- Listeners receive and process notifications
- Connected clients receive notifications via WebSocket/SSE
Feel free to submit issues and enhancement requests!