TalkFlow is a modern, real-time chat and messaging backend API built with Java and Spring Boot 3.5.14. It provides a robust architecture for handling user synchronization, secure communication via WebSockets, and message state management with file upload capabilities. The application is secured using OAuth2 Resource Server configuration with Keycloak integration.
- Real-time Messaging: Powered by Spring WebSocket for instant bi-directional chat communication.
- Secure Authentication: OAuth2/JWT authentication using Keycloak with custom JWT converter (
KeycloakJwtAuthenticationConverter). - User Synchronization: Request filters (
UserSynchronizerFilter&UserSynchronizer) for seamless user mapping and database sync. - Message Management: Full lifecycle support with message states (SENT, DELIVERED, SEEN) and multiple message types.
- File Upload & Media: Support for media file uploads (up to 500MB) with configurable storage path.
- Auditing: Base auditing entity (
BaseAuditingEntity) to track creation and modification timestamps automatically. - Database Migrations: Schema management using Flyway for reliable database versioning.
- Environment Configuration: Easy .env file management using
spring-dotenvlibrary. - Docker Support: Docker Compose setup for quick local development environment (PostgreSQL + Keycloak).
- Backend Framework: Java 17, Spring Boot 3.5.14
- Core Spring Modules: Spring Web, Spring Data JPA, Spring Validation, Spring Security
- Real-time Communication: Spring WebSocket
- Authentication & Authorization: Spring OAuth2 Resource Server, Keycloak 26.0.0
- Database: PostgreSQL 15
- Database Migrations: Flyway with PostgreSQL dialect
- Code Generation: Lombok (automatic getters, setters, constructors)
- Configuration Management: spring-dotenv 4.0.0
- Containerization: Docker & Docker Compose
api/
βββ src/main/java/com/yavuzahmet/talkflow/
β βββ chat/ # Chat management (entities, services, controllers, mappers)
β β βββ Chat.java # Chat entity
β β βββ ChatRepository.java # Database access layer
β β βββ ChatService.java # Business logic
β β βββ ChatController.java # REST endpoints
β β βββ ChatMapper.java # DTO mapping
β β βββ ChatResponse.java # Response DTO
β β βββ ChatConstants.java # Constants
β βββ message/ # Message handling (entities, states, file management)
β β βββ Message.java # Message entity
β β βββ MessageRequest.java # Request DTO
β β βββ MessageResponse.java # Response DTO
β β βββ MessageState.java # Enum (SENT, DELIVERED, SEEN)
β β βββ MessageType.java # Enum (TEXT, MEDIA, etc.)
β β βββ MessageService.java # Business logic
β β βββ MessageController.java # REST endpoints
β β βββ MessageMapper.java # DTO mapping
β β βββ MessageRepository.java # Database access layer
β β βββ MessageConstants.java # Constants
β β βββ FileService.java # File upload handling
β βββ user/ # User management
β β βββ User.java # User entity
β β βββ UserRepository.java # Database access layer
β β βββ UserMapper.java # DTO mapping
β β βββ UserConstants.java # Constants
β βββ security/ # OAuth2 & Keycloak configuration
β β βββ SecurityConfig.java # Spring Security configuration
β β βββ KeycloakJwtAuthenticationConverter.java # JWT token conversion
β βββ interceptor/ # Request/Response filtering
β β βββ UserSynchronizerFilter.java # Filter for user sync
β β βββ UserSynchronizer.java # User sync logic
β βββ common/ # Shared utilities
β β βββ BaseAuditingEntity.java # Base class with audit fields
β β βββ StringResponse.java # Generic response wrapper
β βββ file/ # File utilities
β β βββ FileService.java # File operations
β β βββ FileUtils.java # Utility methods
β βββ TalkflowApiApplication.java # Main application entry point
βββ src/main/resources/
β βββ application.yml # Application configuration
βββ pom.xml # Maven dependencies
βββ .mvn/ # Maven wrapper configuration
docker-compose.yml # Docker setup (PostgreSQL + Keycloak)
.env-example # Environment variables template
- Java 17 or higher
- Maven 3.6+
- Docker & Docker Compose (for local development)
- PostgreSQL 15 (or use Docker Compose)
- Keycloak 26.0.0 (or use Docker Compose)
-
Clone the repository:
git clone <repository-url> cd TalkFlow
-
Create
.envfile from template:cp .env-example .env
-
Configure environment variables (
.env):# Database Configuration DB_USER=talkflow_user DB_PASSWORD=your_secure_password DB_NAME=talkflow_db # Keycloak Configuration KC_ADMIN=admin KC_ADMIN_PASSWORD=admin_password
-
Start Docker Compose services:
docker-compose up -d
This starts:
- PostgreSQL 15 on
localhost:5432 - Keycloak 26.0.0 on
localhost:9090
- PostgreSQL 15 on
-
Build and run the application:
cd api mvn clean install mvn spring-boot:runThe API will be available at
http://localhost:8080
- POST
/api/v1/chats- Create a new chat between two usersPOST /api/v1/chats?sender-id=USER1&receiver-id=USER2 Response: { "response": "CHAT_ID" }
- GET
/api/v1/chats- Get all chats for the authenticated userGET /api/v1/chats Authorization: Bearer <JWT_TOKEN>
- POST
/api/v1/messages- Send a text messagePOST /api/v1/messages Content-Type: application/json { "chat-id": "CHAT_ID", "content": "Message text", "type": "TEXT" } - POST
/api/v1/messages/upload-media- Upload media filePOST /api/v1/messages/upload-media?chat-id=CHAT_ID Content-Type: multipart/form-data file: <binary_file> (max 500MB) Authorization: Bearer <JWT_TOKEN>
- PATCH
/api/v1/messages- Mark messages as seenPATCH /api/v1/messages?chat-id=CHAT_ID Authorization: Bearer <JWT_TOKEN>
- GET
/api/v1/messages/chat/{chat-id}- Retrieve chat messagesGET /api/v1/messages/chat/CHAT_ID
- OAuth2 Resource Server with JWT tokens from Keycloak
- JWT Token Validation: Tokens are validated against Keycloak's issuer-uri (
http://localhost:9090/realms/talkflow) - Custom JWT Converter:
KeycloakJwtAuthenticationConverterextracts claims and roles from tokens - Automatic User Sync:
UserSynchronizerFilterintercepts requests and ensures user data is synced to the database
- All message operations require authentication
- User isolation: Users can only access their own chats and messages
- File upload size limit: 500MB per file
spring:
datasource:
url: jdbc:postgresql://localhost:5432/${DB_NAME}
username: ${DB_USER}
password: ${DB_PASSWORD}
jpa:
hibernate:
ddl-auto: update # Auto-update schema (use 'validate' in production)
show-sql: false
open-in-view: false
database: postgresql
security:
oauth2:
resourceserver:
jwt:
issuer-uri: http://localhost:9090/realms/talkflow
servlet:
multipart:
max-file-size: 500MB
application:
file:
uploads:
media-output-path: ./upload # Media storage path- Flyway is configured but currently disabled (
flyway.enabled: false) - Schema management is handled by Hibernate (
ddl-auto: update) - To enable Flyway migrations: Set
spring.flyway.enabled: trueand add migration files todb/migration/
Run unit tests:
cd api
mvn testid- UUID primary keyusername- Unique username from Keycloakemail- User emailcreated_at- Creation timestamp (audited)updated_at- Last update timestamp (audited)
id- UUID primary keysender_id- Foreign key to Userreceiver_id- Foreign key to Usercreated_at- Creation timestampupdated_at- Last update timestamp
id- UUID primary keychat_id- Foreign key to Chatsender_id- Foreign key to Usercontent- Message texttype- Message type (TEXT, MEDIA)state- Message state (SENT, DELIVERED, SEEN)file_path- Path to uploaded file (if media)created_at- Creation timestampupdated_at- Last update timestamp
All dependencies are specified in api/pom.xml. Key dependencies:
spring-boot-starter-web- REST API supportspring-boot-starter-data-jpa- Database ORMspring-boot-starter-oauth2-resource-server- OAuth2 securityspring-boot-starter-websocket- WebSocket supportorg.postgresql:postgresql- PostgreSQL driverorg.flywaydb:flyway-*- Database migrationsorg.projectlombok:lombok- Code generationme.paulschwarz:spring-dotenv- .env support
mvn dependency:update-check
mvn dependency:tree- Ahmet YAVUZ