I wanted to create something that required me to use databases but I didn’t want to spend a whole lot of money on servers. Since creating server-based applications is pretty hard considering you have to account for scalability, concurrency, load distribution/balancing, database optimization, etc. I just didn’t want to deal with all that stuff because I don’t think I could do a good job alone. Thus, I set my goal to be the creation of a P2P chat application.
What is P2P?
P2P (Peer to Peer) is a serverless networking architecture where the users are both the and the client. Since the users are the ones who hold up the architecture, it is infinitely scalable (kinda). P2P applications like BitTorrent or Skype still need a server that could coordinate connections between users, so they’re not exactly infinitely scalable. They way we implement the P2P anonymous chat is pretty simple, here’s a diagram.
The chat algorithm
- The user registers itself with the server. An entry in the server database is created or updated containing the user’s GUID, IP address, and the number of reports he has gotten.
- Two available users are selected semi-randomly (reports affect selection) and are given the other users IP address.
- The two users establish a P2P connection and can now send messages to each other.
Simplicity of anonymity
Since the chat is anonymous, we don’t need to keep user details like usernames or passwords. Thus, I opted for identification based on the GUID of the device. This makes it easier for the user to log in since he actually doesn’t have to remember account details. Each user has four properties:-
- GUID – The primary key of the database. So we can identify users and store information on them.
- IP Address – Used to establish a P2P connection between the two users.
- Reports – To increase the quality of chat for users who aren’t socially retarded.
- IsAvailable – To get users who are available to chat i.e. not already chatting.
That’s it. Keep a look out for the next part of this article which actually shows the implementation in C#/Xamarin Android.