Melangerie is a real-time chat application built with Rust, Svelte, and Rocket, a web framework for Rust. The main purpose is an in-house alternative to apps like Microsoft Teams and Skype when just used for basic communication. It allows users to create chat rooms, invite other users to join, and send messages within those rooms. The application uses WebSockets for real-time communication and JSON Web Tokens (JWT) for user authentication.
- User registration and authentication using JWT
- Create and join chat rooms
- Real-time messaging within chat rooms
- Add users to existing chat rooms
- List available chat rooms for a user
- Offline message storage and retrieval upon user reconnection
- Reporting mechanism for users to report issues
- Logging of server events and user reports
The project is structured as follows:
office-chat-rs/
├── server/
│ ├── public/
│ │ ├── index.html
│ │ └── ...
│ ├── src/
│ │ ├── auth.rs
│ │ ├── chat.rs
│ │ ├── cors.rs
│ │ ├── log.rs
│ │ ├── main.rs
│ │ ├── test.rs
│ │ └── types.rs
│ └── Cargo.toml
└── client/
└── ...
- The
serverdirectory contains the Rust backend code.auth.rs: Handles user registration, login, and JWT generation/verification.chat.rs: Implements the WebSocket connection handling and chat room functionality.cors.rs: Provides CORS support for the server.log.rs: Defines the logging functionality for server events and user reports.main.rs: The entry point of the application, setting up the Rocket server and routes.test.rs: Contains unit tests for the application.types.rs: Defines the data types used throughout the application.
- The
publicdirectory serves as the static file server, containing the HTML, CSS, and JavaScript files for the frontend.
- Rust (latest stable version)
- Cargo (Rust's package manager)
-
Clone the repository:s
git clone https://github.com/exotik850/melangerie.git -
Change to the
clientdirectory and install dependencies:cd melangerie/client && npm i -
Build the client for the backend to serve:
npm run build -
Change to the
serverdirectory:cd ../server -
Set up the environment variables:
- Create a
.envfile in theserverdirectory. - Add the following variables to the
.envfile:ReplaceJWT_SECRET=your-jwt-secretyour-jwt-secretwith a secure secret key for JWT generation and verification.
- Create a
-
Run the server:
cargo runThe server will start running at
http://0.0.0.0:8080.
POST /auth/login: User login endpoint. Expects a JSON payload withnameandpasswordfields.POST /auth/createuser: User registration endpoint. Expects a JSON payload withnameandpasswordfields.GET /auth/checkuser/<name>: Checks if a user with the givennameexists.GET /chat/connect: WebSocket endpoint for establishing a chat connection.GET /chat/list: Lists the available chat rooms for the authenticated user.POST /chat/adduser/<room>/<user_id>: Adds a user with the givenuser_idto the specifiedroom.POST /chat/chatroom: Sends a message to a chat room. Expects a JSON payload withsender,room,content, andtimestampfields.POST /chat/create/<name>/<users..>: Creates a new chat room with the givennameand initialusers.GET /<file..>: Serves static files from thepublicdirectory.POST /report: Endpoint for users to report issues. Expects a JSON payload withnameandissuefields.
The application uses JSON Web Tokens (JWT) for user authentication. When a user logs in or registers, a JWT is generated and sent to the client. The client must include this token in the Authorization header for subsequent requests that require authentication.
The WebSocket endpoint (/chat/connect) handles real-time communication between the server and clients. Upon establishing a connection, the server authenticates the user using the provided JWT. Once authenticated, the user can join chat rooms, send messages, and receive messages from other users in real-time.
The WebSocket messages are JSON-encoded and follow a specific structure defined by the UserAction and ServerAction enums in types.rs.
The application includes a logging mechanism to log server events and user reports. The Log struct in log.rs handles writing log messages to a file named log.txt. The server periodically flushes the log buffer to ensure that logs are persisted.
Contributions to the project are welcome! If you find any bugs or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License.
