This project is an API built in Java using the Spring Boot framework. It serves as the backend for the RT-Chat Frontend project, providing real-time chat functionalities.
- Framework: Java 17, Spring Boot 3
- Database: PostgreSQL
- Authentication: Spring Security & JSON Web Tokens (JWT)
- Real-time Communication: WebSocket (STOMP)
- API Documentation: Swagger/OpenAPI
- Unit Tests: JUnit & Mockito
- Integration Tests: Spring Boot Test
- Prerequisites
- Installation
- Configuration
- Running the application
- Authentication Flow
- API Endpoints Overview
- WebSocket Communication
- Database Schema
Before starting, ensure you have the following tools installed on your system:
- Java 17 or higher
- Apache Maven 3.6 or later.
- PostgreSQL server running locally or accessible remotely.
- Clone the repository:
git clone https://github.com/jmfs12/rt-chat-api.git
cd rt-chat-api- Install dependencies with Maven
mvn clean installApplication settings are managed in src/main/resources/application.properties. Before running the application, you must configure the database connection and JWT settings.
-
Database configuration
- Set up your PostgreSQL database and update the following properties in application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/your_database spring.datasource.username=your_username spring.datasource.password=your_password spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
-
JWT Secret Configuration
- Set a secure secret key for JWT in the application properties:
# JWT Configuration # Use a strong base64 encoded secret key for production environments. jwt.secret.key=YOUR_ULTRA_SECRET_KEY_FOR_JWT_SIGNING jwt.expiration.ms=86400000 # Token expiration time in milliseconds (e.g., 24 hours)
Once configured, start the application using the Spring Boot Maven plugin:
mvn spring-boot:runThe Api will be accessible at http://localhost:8080.
API documentation is available at http://localhost:8080/swagger-ui.html
once the application is running.
Authentication is handled using JWT. Access to protected endpoints requires a valid token provided in the Authorization header.
- Registration: Users can register by sending a POST request to
/api/auth/registerwith their details. - Login: The user authenticates using credentials via the POST
/api/auth/loginendpoint. - Token Generation: Upon successful authentication, a JWT is generated and returned to the client.
- Token Usage: The client includes the JWT in the Authorization header for subsequent requests to protected endpoints.
- Authentication:
POST /api/auth/register: Register a new user.POST /api/auth/login: Authenticate and receive a JWT.
- User Management:
GET /api/users: Retrieve a list of users (protected).GET /api/users/me: Get current user details by ID (protected).
- Message Management:
GET /api/messages: Get all messages between two users.
Real-time messaging is implemented using WebSocket with STOMP protocol.
- WebSocket Endpoint:
ws://localhost:8080/ws - User-Specific Notifications (Private Messages/Updates): Clients subscribe to their private queue to receive direct messages and notifications.
- Subscribe: /user/queue/message
The database schema is managed by Spring Data JPA based on the entity classes defined in the source code. The primary entities include:
- User: Represents application users with fields for username, password, email, and roles.
- Message: Represents chat messages with fields for sender, receiver, content, and timestamp
Contributions are welcome! Please fork the repository and create a pull request with your changes.