You can follow the step from the demo to implement the p2p chat or read the step starting from the 'Instruction' below
The code that allow other users to connect to receive messages sent from those connected users
The code that allow a user connect to a specific user, and then send messages to that user.
So a user will need 2 windows, one runs Receiver.py, to receive messages from other users, and one runs Sender.py, to send message to the a user this user connects to
A module that creates a local database and store messages. Sender.py use this module to insert sent and unsent message. The database is to store all the message a current user wants to send, and for the messages current user wants to send to a target user that is offline, they will be stored in the local database first. When the target user is online, and the current user is online, current user will automatically send those message to that target user before sending any message.
This is the discover module that has all the endpoints to manage the users. This is a FastAPI that we wrote.
This is the python code that our Receiver.py and Sender.py use to interact with the discover_api.py FastAPI. Because FastAPI is not like class and function, so we create the discovery.py to have a class, so we can use the python code to interact with the API
For communication, 2 users connect to each other by connecting them with their host and port, a middle server is not used. During the demo, when I enter message, the discover server's API is called. This is because I am calling to the discover server to check if the any of them is still online. It is not used as a messenger to pass messages between 2 users.
- clone this repository
- Go into the p2p-chat folder from terminal: cd p2p-chat
- uvicorn discover_api:app --reload
- python3 Receiver.py
- Enter '127.0.0.1' when prompted "Enter discovery_api server's host:"
- Enter '8000' when prompted "Enter discovery_api server's port:"
- Enter 'Sam' when prompted "Enter your username:"
It will say "You don't exists in the database. Now we are creating a new account for you.", because you are running this program the first time, and the database doesn't have any user, including "Sam" - Enter '127.0.0.1' when prompted "Enter your host:"
- Enter '4444' or a port number you like when prompted "Enter your port:"
- Now you are online, and you can be connected by other users.
- python3 Receiver.py
- Enter '127.0.0.1' when prompted "Enter discovery_api server's host:"
- Enter '8000' when prompted "Enter discovery_api server's port:"
- Enter 'Carol' when prompted "Enter your username:"
It will say "You don't exists in the database. Now we are creating a new account for you.", because Carol is a new user, and it doesn't exist in the database - Enter '127.0.0.1' when prompted "Enter your host:"
- Enter '5555' or a port number you like when prompted "Enter your port:", just make sure it's a different one than Sam's port
- Now you are online, and you can be connected by other users.
-
python3 Sender.py
-
Enter "Sam" when prompted "Enter your username (Your username must be the same as your username used in the Receiver): "
NOTE: Make sure the name is the same as you created for the Sam receiver -
Enter '127.0.0.1' when prompted: "Enter discovery_api server's host:"
-
Enter '8000' when prompted: "Enter discovery_api server's port:"
Now the discover server will display you the users that you can talk to, both online and offline. You will see the following message:
users:
Carol: {'username': 'Carol', 'status': 'online', 'ip_address': '127.0.0.1', 'port': 5555} -
Enter 'Carol' when prompted: "Enter the username that you want to talk to:"
-
Now your sender window is connected to Carol, and Carol's Receiver window will display "Sam just connected to you!"
-
You can enter any thing press enter key to send messages
-
python3 Sender.py
-
Enter "Carol" when prompted "Enter your username (Your username must be the same as your username used in the Receiver): "
NOTE: Make sure the name is the same as you created for the Carol receiver -
Enter '127.0.0.1' when prompted: "Enter discovery_api server's host:"
-
Enter '8000' when prompted: "Enter discovery_api server's port:"
Now the discover server will display you the users that you can talk to, both online and offline. You will see the following message:
users:
Sam: {'username': 'Sam', 'status': 'online', 'ip_address': '127.0.0.1', 'port': 5555} -
Enter 'Sam' when prompted: "Enter the username that you want to talk to:"
-
Now your sender window is connected to Sam, and Sam's Receiver window will display "Carol just connected to you!"
-
You can enter any thing press enter key to send messages
Now, Sam has two window, one is runs Receiver.py, one runs Sender.py. The Receiver is to display messages sent from users that connect to Sam, and the Sender is for Sam to send message to the user Sam connects to
Carol has the same things, one window runs Receiver.py, one runs Sender.py. The Receiver is to display message sent from users that connect to Carol, and the Sender is for Carol to send message to the user Carol connects to
Right now, Sam can type things in the window that runs his Sender.py and send them, then the message will display in Carol's window that runs Receiver.py
Also, Carol can type things in the window that runs her Sender.py and send them, then the message will display in Sam's window that runs Receiver.py
- press 'control c' in Carol's Receiver window, to use KeyboardInterrupt to stop Carol's programs, to simulate she's offline. Don't close Carol's Receiver window
- In Sam's sender window, if you type something, it will display:
"Carol is offline right now. You are in offline sending mode. You can enter messages and they will be sent once the user is online, or type 'exit' to talk to another user." - Enter anything you want when prompted: "msg: "
- Enter 'exit' when prompted: "msg: " to exit offline sending mode
Now Sam's sender window program is terminated. Run the following command lines to bring back Sam's sender program: - Don't close the window
- python3 Receiver.py
- Enter '127.0.0.1' when prompted: "Enter discovery_api server's host: "
- Enter '8000' when prompted: "Enter discovery_api server's port: "
- Enter "Carol" when prompted: "Enter your username: "
- Enter "127.0.0.1" when prompted: "Enter your host: "
- Enter "5555" or the any port you want when prompted: "Enter your port: ". Just make sure the port is not the same as Sam's port
- It will display "You are online. Waiting for other users to connect to you..."
Step 3: bring back Sam's sender window, and connect to Carol. Once Sam's sender window connects to Carol's window, all the messages that was typed in Step 1.3 will be sent to Carol, and you are not even typing.
- python3 Sender.py
- Enter 'Sam' when prompted: "Enter your username (Your username must be the same as your username used in the Receiver):"
- Enter '127.0.0.1' when prompted: "Enter discovery_api server's host:"
- Enter '8000' when prompted: "Enter discovery_api server's port:"
It will display:
users: Carol: {'username': 'Carol', 'status': 'online', 'ip_address': '127.0.0.1', 'port': 5555} - Enter 'Carol' when prompted: "Enter the username that you want to talk to: "
- In Carol's receiver window we created in Step 2, it will display:
Sam just connected to you!
Offline message from Sam
messages...