Chat application use gRPC to communicate between processes.
Demo • Documentation • Report Bug • Request Feature
📖 Table of Contents
Check out the demo video to see the app in action.
Check out the report to see full report.
- Python:
>= 3.10.7
- gRPC tools: gRPC compiler, Install here.
1. Define your gRPC service using protocol buffers. This will define the messages and methods used for communication.
-
Define your service in a .proto file using protocol buffer syntax. (
chat.proto
in/service
)syntax = "proto3"; service ChatService { rpc SendMessage(Message) returns (Message) {} rpc ReceiveMessage(Empty) returns (stream Message) {} } message Message { string user_name = 1; string text = 2; } message Empty {}
Explain variable
This defines a:
- Message type with
text
andsender
fields - ChatService with two methods:
SendMessage
takes aMessage
object as input and returns aMessage
objectReceiveMessage
takes an emptyEmpty
object as input and returns a stream ofMessage
objects.
- Message type with
-
Use the generated code to implement your gRPC service.
python -m grpc_tools.protoc -I /path/to/protos --python_out=. --grpc_python_out=. /path/to/protos/my_service.proto
Example:
python3 -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. service/my_service.proto
-
Implement the server-side code for your gRPC service. This will handle incoming requests and provide responses. You can create a new process for each instance of your gRPC server.
Explain server
-
This defines a
ChatServiceServicer
class that implements theChatService
defined inchat.proto
. TheSendMessage
function appends the received message to a list of messages and returns the same message. TheReceiveMessage
function yields all the messages in the list. -
The
serve
function creates a gRPC server and adds theChatServiceServicer
to it. It starts the server on port50051
.
-
-
Implement the client-side code for your gRPC service. This will send requests to the server and receive responses. You can create one or more client processes as needed.
-
Start the
server process(es)
andclient process(es)
. -
Use gRPC's built-in functionality to handle the communication between the server and client processes.
Install application
# Clone this repository
git clone https://github.com/HCMUS-Project/ChatGRPC.git
# Go into the repository
cd ChatGRPC
Run server
python server.py
Run client (open new terminal)
python client.py
Note If you're using Linux Bash for Windows, see this guide or use
node
from the command prompt.
LIKE reply: for allow user XX to continue chat (at least 2 LIKE)
Syntax: LIKE_<user_id>
Rule:
- Only LIKE once per user
- Can not LIKE your self
Example: LIKE_01
Syntax: [<type>] <content>_<user_id>
Program exception:
- [WARNING] You can not LIKE yourself_
<from_user>
! - [WARNING] You only LIKED: [
<user_id>
]'s message ONCE!_<from_user>
- [WARNING] You are NOT allowed to send message_
<from_user>
- [INFO] You LIKED: [
<user_id>
]'s message_<from_user>
Syntax: [<time>] <content>
<!-- logfile.log exapmle -->
[15:47:47] Quan join group chat - ID(01)
[15:47:54] User[01] send message 'hello'
[15:47:54] User[01] is BLOCKED to send message
[15:48:08] User[01] is not allow to send message
[15:48:15] Van join group chat - ID(02)
[15:48:20] Lien join group chat - ID(03)
[15:48:23] Hao join group chat - ID(04)
[15:48:29] Dat join group chat - ID(05)
[15:48:35] User[05] send message 'hello'
[15:48:35] User[05] is BLOCKED to send message
[15:48:42] User[04] send message 'chao moi nguoi'
[15:48:42] User[04] is BLOCKED to send message
[15:48:51] User[03] send message 'alo alo'
[15:48:51] User[03] is BLOCKED to send message
[15:49:03] User[03] like for User[01]
[15:49:15] User[03] like for User[01]
[15:49:28] User[03] like for User[03]
[15:49:36] User[04] like for User[01]
[15:49:36] User[01] is ALLOWED to send message
[15:49:56] User[01] send message 'hi , i am free'
[15:49:56] User[01] is BLOCKED to send message
[15:50:00] User[01] is not allow to send message
- Client connect and enter username:
<username> join group chat - ID(<user_id>)
- Client send msg success:
User[<user_id>] send message ‘<message>
- Client A like client B's msg:
User[<userA_id>] like for User[<userB_id>]
- Client being block send msg but still try sending msg:
User[<user_id>] is not allow to send message
- Client has been blocked from sending messages:
User[<user_id>] is BLOCKED to send message
- The client is allowed to send messages:
User[<user_id>] is ALLOWED to send message
- Complete the server
- Complete the client
- Log file
- Report
Contributions are always welcome!
This software uses the following open source packages:
Distributed under the MIT License. See LICENSE
for more information.
Bento @quanblue · GitHub @QuanBlue · Gmail [email protected]