Distributed-Instant-Messaging-System-Development is a real-time chat application built using C++17, Boost FFmpeg, OpenGL and gRPC, featuring a distributed TCP server architecture. file transfer and Real time video streaming functionalities are still testing.
- Developed a chat dialog using Qt, using
QListWidget
for an efficient chat record list and combiningQGridLayout
andQPainter
for customized chat bubble styling to enhance the user experience. - Encapsulated Qt Network modules to support HTTP and C/S service communication.
- Implemented core features such as adding friends, friend communication, chat record and chat record status display.
Integrated file transfer functionality to allow users to upload and download files.Implemented an intuitive file management interface with drag-and-drop support.
Backend Architecture Design:
-
Designed a distributed service architecture with the following components:
**gateway-server**
(Gateway Service): Provides HTTP APIs to handle user login, registration, and authentication.**chatting-server**
(Distributed Chatting Service): Utilized ASIO to implement efficient TCP long connection communication.**balance-server**
(Load Balancing Service): Allocates chat services dynamically to achieve load balancing.**captcha-server**
(Captcha Service): Generates and validates captchas for enhanced security.**resources-server**
(Distributed Resources Service): Manages file storage, supporting file uploads and downloads.
-
Enabled inter-service communication using the gRPC protocol, ensuring high availability and support for reconnections.
High-Performance Optimization:
- Implemented multithreading with
io_context
pools in thechatting-server
to boost concurrent performance. - Developed a MySQL connection pool to manage user data, friend relationships, and chat records.
- Designed a Redis connection pool for caching optimization.
- Built a gRPC connection pool to enhance distributed service access efficiency.
- File transfers are managed using the
resources-server
. - Implemented chunked file uploads to handle large files efficiently.
- Supported resumable uploads and downloads using unique identifiers shared by
chatting service
Enhanced file security with authentication checks and encryption.
Technical Highlights:
- Gateway service provides stateless HTTP interfaces and integrates load balancing functionality.
- Chat service supports asynchronous message forwarding with reliable TCP long connections.
- Resources service supports Flow Control
andRTMP
real time streaming protocol - Achieved support for 8k~10k+ concurrent connections on a single server.
- Support deferred connection cleanup process, ensuring all data are transmitted before closing the connection
Captcha-server imported ioredis
, grpc-js
, pproto-loader
, nodemailer
, uuidv4
libraries to the project.
Manages load balancing and server resource allocation.
Responsible for storing user-uploaded files and ensuring secure file access.
-
User Login
(SERVICE_LOGINSERVER)
-
User Logout
SERVICE_LOGOUTSERVER
-
User Search For Peers
(SERVICE_SEARCHUSERNAME)
-
Create Private Chat
Thread ID
(SERVICE_CREATENEWPRIVATECHAT
) -
Pull Chat Thread By Page
(SERVICE_PULLCHATTHREAD)
-
Pull Chat Record By Page
(SERVICE_PULLCHATRECORD)
-
User Who Initiated Friend Request
(SERVICE_FRIENDREQUESTSENDER)
-
User Who Received Friend Request
(SERVICE_FRIENDREQUESTCONFIRM)
-
User Sends Text Msg To It's Server
(SERVICE_TEXTCHATMSGREQUEST)
-
Server verifies Text Msg and allocate a
Message ID
as a confirmation(SERVICE_TEXTCHATMSGRESPONSE)
-
Server(It could be another server) forwards Text Msg to target user
(SERVICE_TEXTCHATMSGICOMINGREQUEST)
-
Client Terminal Sends Heart Beat
(SERVICE_HEARTBEAT_REQUEST)
All services are using HTTP short connections, users are going to create a POST method to the gateway-server and gateway-server is going to respond to the client requests accordingly.
-
/get_verification
User sends a email address to gateway-server and request to get a Email verification code(CPATCHA) request to server. server using gRPC protocol to communicate with NodeJS server(
captcha-server
) and create an unique uuid for the user. The uuid is going to store in a Redis memory database with a timeout setting, user should register the new account within the valid time or request for a new one instead. -
/post_registration
After request for a valid CPATCHA, user could trigger registration confirm button to post registration request to the server. Server will whether this user's identity is collision with any other user inside the system, if no collision found the info will be stored inside database.
however, SQL injection protection mechanism is still not available yet! -
/check_accountexists
After account registration, when user demands to change his/her password, we have to verifiy the account existance.
-
/reset_password
After executing
/check_accountexists
process, then user could enter his/her new password info, and client terminal could send the new password info to the the server. server will do the similiar process in/post_registration
and alter the existing data inside the database. -
/trylogin_server
please be careful,
trylogin_server
could not login into the real server directly. It's a server relay!The identification is similiar to
/check_accountexists
authenication process. Thegateway-server
will communicate withbalance-server
for the address ofchatting-server
by using gRPC, andchatting-server
will do load-balancing and return the lowest load server info back. However, The user connection status will not maintained and managed bygateway-server
andgateway-server
doesn't care about this either, client will receive the real address ofchatting-server
and connecting to it by itself.however, SQL injection protection mechanism is still not available yet!
Ensure you have the following installed:
-
Docker for container management
-
Redis for caching
-
MySQL for user and file metadata storage
-
FFmpeg -
OpenGL
It's strongly suggested to use docker to build up those services ^_^. If you intended to pass a host directory, please use absolute path.
-
Create a local volume on host machine and editing configuration files. Please don't forget to change your password.
mkdir /absolute_path_to/conf/ cp ./DistributedIMSystem/conf/redis.conf /absolute_path_to/conf/redis.conf
-
Creating a
Redis
container and execute following commands.docker pull redis:7.2.4 #Pull the official docker image from Docker hub docker run \ --restart always \ -p 16379:6379 --name redis \ --privileged=true \ -v /absolute_path_to/conf/redis.conf:/etc/redis/redis.conf \ -v /absolute_path_to/data:/data:rw \ -d redis:7.2.4 redis-server /etc/redis/redis.conf \ --appendonly yes
-
Entering
Redis
container and access to command lineredis-cli
.docker exec -it redis bash #entering redis redis-cli #login redis db
-
Create a local volume on host machine and editing configration files. Please don't forget to change your password.
#if you are using windows, please download WSL2 mkdir -p /absolute_path_to/mysql/{conf,data} cp ./DistributedIMSystem/conf/mysql.cnf /absolute_path_to/conf/mysql.cnf
-
Creating a
MySQL
container and execute following commands.docker pull mysql:8.0 #Pull the official docker image from Docker hub docker run --restart=on-failure:3 -d \ -v /absolute_path_to/mysql/conf:/etc/mysql/conf.d \ -v /absolute_path_to/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD="your_password" \ -p 3307:3306 --name "your_container_name" \ mysql:8.0
-
Entering
MySQL
container and access tomysql
command line.docker exec -it "your_container_name" bash #entering mysql mysql -uroot -p"your_password" #login mysql db ( -u: root by default, -p password)
-
Initialize
MySQL
database with followingSQL
commands to create DB and table schemas../DistributedIMSystem/conf/DistributedIMSystem.sql
Most of those basic configurations are using *.ini file, except Captcha-server
.
Windows, Linux, MacOS(Intel & Apple Silicon M)
git clone https://github.com/Liupeter01/Distributed-Instant-Messaging-System-Development
git submodule update --init --recursive
cd Distributed-Instant-Messaging-System-Development
cmake -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_INCLUDE_PATH=/usr/local/include -DCMAKE_CXX_FLAGS=-03
cmake --build build --parallel [x] --target all
[IMPORTANT]: you have to start those services first!!
.\build\debug\balance-server\LoadBalanceServer
[IMPORTANT]: If Chatting-server not started , then it will resulting in NO_AVAILABLE_CHATTING_SERVER
ERROR
.\build\debug\chatting-server\ChattingServer
.\build\debug\resources-server\ResourcesServer
cd captcha-server
npm install
node index.js
.\build\debug\gateway-server\GatewayServer
SyntaxError: Unexpected token in JSON at position 0
at JSON.parse (<anonymous>)
Solving please change your encoding method to UTF-8, especially for VSCode user
Referring Url https://stackoverflow.com/questions/55960919/nodejs-syntaxerror-unexpected-token-in-json-at-position-0
set(protobuf_BUILD_LIBUPB OFF)
Referring Url grpc/grpc#35794
-
Download icu 74.1
git clone https://github.com/unicode-org/icu.git
-
Compile and Install
git clone https://github.com/unicode-org/icu.git cd icu/source ./configure && make -j[x] sudo make install
-
setup cmake variable
cmake -Bbuild -DCMAKE_INCLUDE_PATH=/usr/local/include cmake --build build --parallel x
Referring Url https://unicode-org.github.io/icu/userguide/icu4c/build.html
set(OPENSSL_NO_ASM ON)
Referring Url grpc/grpc#16376
CMake Error: install(EXPORT "protobuf-targets" ...) includes target "libprotobuf-lite" which requires target "absl_node_hash_map" that is not in any export set.
set(ABSL_ENABLE_INSTALL ON)
Referring Url protocolbuffers/protobuf#12185 protocolbuffers/protobuf#12185 (comment)
you have to start the main server first and then open nodejs service
Add those codes in front of FetchContent to prevent inclusion issue.
if(MSVC)
message(STATUS "MSVC detected, enabling /EHsc")
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} /EHsc"
CACHE STRING "MSVC exception flag" FORCE)
add_compile_options(/EHsc)
endif()
Because of efficiency issue, Currently, there is no log system on resources-server