<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PeerBrain Archives - Be on the Right Side of Change</title>
	<atom:link href="https://blog.finxter.com/category/peerbrain/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.finxter.com/category/peerbrain/</link>
	<description></description>
	<lastBuildDate>Fri, 10 Mar 2023 08:38:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.finxter.com/wp-content/uploads/2020/08/cropped-cropped-finxter_nobackground-32x32.png</url>
	<title>PeerBrain Archives - Be on the Right Side of Change</title>
	<link>https://blog.finxter.com/category/peerbrain/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>PeerBrain &#8211; A Decentralized P2P Social Brain Network App</title>
		<link>https://blog.finxter.com/peerbrain-a-decentralized-p2p-social-brain-network-app/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Fri, 10 Mar 2023 08:32:07 +0000</pubDate>
				<category><![CDATA[Peer to Peer Systems]]></category>
		<category><![CDATA[PeerBrain]]></category>
		<category><![CDATA[Projects]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1197012</guid>

					<description><![CDATA[<p>The post <a href="https://blog.finxter.com/peerbrain-a-decentralized-p2p-social-brain-network-app/">PeerBrain &#8211; A Decentralized P2P Social Brain Network App</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="[Launching] Peer Brain - A Decentralized P2P Social Network App" width="937" height="527" src="https://www.youtube.com/embed/GaQGfzTiHTc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<ul class="wp-block-list">
<li>Join Discord: <a href="https://discord.com/invite/Cn7jWhTckP" target="_blank" rel="noreferrer noopener">https://discord.com/invite/Cn7jWhTckP</a></li>



<li>Join GitHub: <a href="https://github.com/shandralor/PeerBrain" target="_blank" rel="noreferrer noopener">https://github.com/shandralor/PeerBrain</a></li>
</ul>
<p>The post <a href="https://blog.finxter.com/peerbrain-a-decentralized-p2p-social-brain-network-app/">PeerBrain &#8211; A Decentralized P2P Social Brain Network App</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>The PeerBrain Vision &#8211; A Decentralized P2P Social Brain Network</title>
		<link>https://blog.finxter.com/the-peerbrain-vision-a-decentralized-p2p-social-brain-network/</link>
		
		<dc:creator><![CDATA[Tom Teck]]></dc:creator>
		<pubDate>Thu, 09 Mar 2023 17:12:32 +0000</pubDate>
				<category><![CDATA[Peer to Peer Systems]]></category>
		<category><![CDATA[PeerBrain]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1194572</guid>

					<description><![CDATA[<p>&#160;Dear Finxter community, Most of you will not know me, so I’ll give you a quick introduction. About Me My name is Tom, and for the last 6 months, I’ve been teaching myself programming. Primarily Python with some web frameworks (Flask, FastAPI, Streamlit) thrown in for good measure. I’ve found I love the way you ... <a title="The PeerBrain Vision &#8211; A Decentralized P2P Social Brain Network" class="read-more" href="https://blog.finxter.com/the-peerbrain-vision-a-decentralized-p2p-social-brain-network/" aria-label="Read more about The PeerBrain Vision &#8211; A Decentralized P2P Social Brain Network">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/the-peerbrain-vision-a-decentralized-p2p-social-brain-network/">The PeerBrain Vision &#8211; A Decentralized P2P Social Brain Network</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>&nbsp;Dear Finxter community,</p>



<p>Most of you will not know me, so I’ll give you a quick introduction.</p>



<h2 class="wp-block-heading">About Me</h2>



<p>My name is Tom, and for the last 6 months, I’ve been teaching myself programming. Primarily Python with some web frameworks (Flask, FastAPI, Streamlit) thrown in for good measure. I’ve found I love the way you can create something new from an idea or even a stray thought. That fact is also what drove me to start learning programming in the first place.</p>



<p>Every one of us knows the feeling that a certain solution for a certain problem doesn’t quite cover all its bases. If only you could adapt or change a few things… That feeling, that urge to solve the issue, is what drives me. And as I grow more adept in this, I admit that the satisfaction gained from it keeps growing also.</p>



<p>It is the drive to learn that brought me to the Finxter blog and academy. I had been scouring the web, looking for learning platforms, tutorials, etc. After opening the site for the first time and having a quick read, the community aspect immediately appealed to me. The way it both has a thriving community while also being driven by it kind of puts it in its own category. I am quite certain that is why I decided to join the mailing list, looking back.</p>



<p>Fast forward to the start of 2023. Chris sent out the first of his 100 programming challenges for the new year. I jumped on the first challenge to create an <a href="https://blog.finxter.com/how-i-created-a-secure-password-and-passphrase-generator-in-streamlit/" data-type="post" data-id="1113906" target="_blank" rel="noreferrer noopener">interactive web app</a>. I was learning Streamlit at the time, and my application made the shortlist. The email that contained that shortlist also first mentioned the idea for an application created by the community. I got hooked on that idea immediately. I shot off a quick email to Chris saying I would like to take part in this very much, whatever it turned out to be. Little did I know what I had gotten myself into <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h2 class="wp-block-heading">The PeerBrain Vision &#8211; A Decentralized P2P Social Brain Network</h2>



<p>It was a couple of weeks later when Finxter sent out the update email with the idea for Peerbrain/Brainwaves. A decentralized P2P application for sharing messages/tweets. An application without possible interference from governments or companies. I loved it! </p>



<p>The only problem was I had no idea in hell how to go about it. So I started researching existing applications or frameworks that were trying to achieve this. The <a href="https://matrix.org/" data-type="URL" data-id="https://matrix.org/" target="_blank" rel="noreferrer noopener">Matrix project</a> is the biggest one. I dove into the documentation and came out knowing two things. The guys who designed this are very good at what they do. The other thing was that I did not even understand half of what I would need if I was to try and use it. I also did not like using something that was already far along in development if I look back now. I wanted to be able to go through the entire process and see how every part of it worked.</p>



<p>After this, I decided that I could not find something that suited the needs I felt the project had. So I started designing and developing it myself. When brainstorming about how the application should work, I realized we had a problem. An application used by thousands or more people needs an always online component. There needs to be a sort of “base” that will make sure everyone that wants to connect to someone else can do so.</p>



<h2 class="wp-block-heading">Finding Inspiration in Torrents</h2>



<p>I found a lot of inspiration in how torrents work. The torrent tracker is a server that facilitates communication. It is an intermediate between the users (peers) who want to share files with one another. The actual sharing of those files is a P2P process. But the process would not be possible without the help of the trackers.</p>



<p>I transferred this concept to the current project. It meant we needed a server that would allow users to communicate with each other. As Python is my programming language of choice, I needed something that could do this. After some more research, I found the <a rel="noreferrer noopener" href="https://blog.finxter.com/my-journey-to-help-build-a-p2p-social-network-database-code-structure/" data-type="post" data-id="1188982" target="_blank">FastAPI framework</a>. It fit all my requirements for a server, and I decided to give it a go. As I had never used it before, more research became my next goal. I am sure some of you will spot a trend by now<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<p>After a few days of coding, and some emails back and forth between Chris and myself, I started to realize some things. </p>



<p>The first was that this project would be way more complex than I ever imagined. I didn’t and still don’t really feel comfortable trying to build it.  I do find that solving certain problems keeps giving me the motivation to continue. As long as this is the case, I will find the motivation to step over my impostor syndrome.</p>



<p>Another thing, and this one is more important, is that the P2P model would not work. Or at least not the part of storing the messages on local machines and not in the cloud. We live in a time when people log in somewhere with certain expectations. They want all their settings already in place, their contacts synced, and their data ready to consume. Storing everything locally would be a huge step back in their eyes. And I am quite sure that if we want this to succeed, we’ll need a lot of people! So it was back to the drawing board to change the application logic to consider this.</p>



<h2 class="wp-block-heading">Where to Store Messages?</h2>



<p>The first thing I am focusing on is the possibility of creating messages/tweets that your friends can read. The solution I found was that storing the messages locally to avoid tampering with them was not viable. Because of this, the messages themselves needed to be protected. If the messages exist on a server, we need to take this into account. This means making sure that even someone who controls the server could not read the contents. This means encryption and, more specifically, end-to-end encryption.</p>



<p>Let me tell you all, encryption is complicated. </p>



<p>I’m sure I am still only skimming the surface of it, but I found a working solution. I got inspiration from how both Apple and Owncloud encrypt user data. In short, the application will use both an RSA keypair and a symmetric key to ensure the security of the messages. The original idea was to only use one API server but this started to pose many problems almost immediately. Messages need to be always available. And new friends want to read messages created before they become friends with the user who created them.</p>



<p>So the solution for this was to split the functionality. </p>



<p>The main API server will store the encrypted messages and user objects. Within those objects, we define the user parameters and the friend relationships. It will handle authentication and user creation. </p>



<p>The second server will hold an object per user that contains the following: a username, a user password hash, their public key and their unencrypted symmetric key. This key is what gets used to encrypt any message the user creates. </p>



<p>Once a user has requested an encrypted copy of a friend’s symmetric key, this will also live within the object, to make future requests more efficient. Otherwise, the second server would need to re-encrypt that key every time a request comes through.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="739" height="939" src="https://blog.finxter.com/wp-content/uploads/2023/03/image.jpeg" alt="" class="wp-image-1194580" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image.jpeg 739w, https://blog.finxter.com/wp-content/uploads/2023/03/image-236x300.jpeg 236w" sizes="(max-width: 739px) 100vw, 739px" /></figure>



<p>And it is at the end of this very long explanation that my question comes to you, dear Finxter community. </p>



<p>We, a few developers who have already joined, need your help. To develop this further, we need people who can test. And more important, people that break the application! We need to iron out the bugs and test it under load. </p>



<h2 class="wp-block-heading">Clone the GitHub &amp; Join Discord <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h2>



<p>That is why we would appreciate it if you clone the <a href="https://github.com/shandralor/PeerBrain" data-type="URL" data-id="https://github.com/shandralor/PeerBrain" target="_blank" rel="noreferrer noopener">Github repo</a> and try to run the client application. We need people to register an account, add friends and create and read messages.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9d1-200d-1f4bb.png" alt="🧑‍💻" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>GitHub</strong>: <a href="https://github.com/shandralor/PeerBrain">https://github.com/shandralor/PeerBrain</a></p>



<p>If you feel like this applies to you, please <a href="https://discord.com/invite/Cn7jWhTckP" data-type="URL" data-id="https://discord.com/invite/Cn7jWhTckP" target="_blank" rel="noreferrer noopener">join the discord server</a>! You can find all the resources there. You can also ask any question that you have.</p>



<p>I would like to thank you all for reading this and helping us out if you want to!</p>



<p>Sincerely,</p>



<p>Team Peerbrain</p>



<p></p>
<p>The post <a href="https://blog.finxter.com/the-peerbrain-vision-a-decentralized-p2p-social-brain-network/">The PeerBrain Vision &#8211; A Decentralized P2P Social Brain Network</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>My Journey to Help Build a P2P Social Network &#8211; Database Code Structure</title>
		<link>https://blog.finxter.com/my-journey-to-help-build-a-p2p-social-network-database-code-structure/</link>
		
		<dc:creator><![CDATA[Tom Teck]]></dc:creator>
		<pubDate>Tue, 07 Mar 2023 19:49:04 +0000</pubDate>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Peer to Peer Systems]]></category>
		<category><![CDATA[PeerBrain]]></category>
		<category><![CDATA[Projects]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1188982</guid>

					<description><![CDATA[<p>Welcome to part 3 of this series, and thank you for sticking around! I’ve come to realize this might become a rather long series. The main reason for that is that it documents two things. This is the birth of an application and my personal journey in developing that application. I know parts 1 and ... <a title="My Journey to Help Build a P2P Social Network &#8211; Database Code Structure" class="read-more" href="https://blog.finxter.com/my-journey-to-help-build-a-p2p-social-network-database-code-structure/" aria-label="Read more about My Journey to Help Build a P2P Social Network &#8211; Database Code Structure">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/my-journey-to-help-build-a-p2p-social-network-database-code-structure/">My Journey to Help Build a P2P Social Network &#8211; Database Code Structure</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Welcome to part 3 of this series, and thank you for sticking around! </p>



<ul class="wp-block-list">
<li><strong>Part 1</strong>: <a href="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/" data-type="URL" data-id="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/" target="_blank" rel="noreferrer noopener">The P2P Bootstrapping Problem – Starting My Journey To Help Build a P2P Social Network</a></li>



<li><strong>Part 2</strong>: <a href="https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/" data-type="URL" data-id="https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/" target="_blank" rel="noreferrer noopener">BrainWaves P2P Social Network – How I Created a Basic Server</a></li>
</ul>



<p>I’ve come to realize this might become a rather long series. The main reason for that is that it documents two things. This is the birth of an application and my personal journey in developing that application. I know parts 1 and 2 have been very wordy. This will change now. I promise that you will see a lot of code in this episode :-).</p>



<h2 class="wp-block-heading">Database Code</h2>



<p>So after that slight philosophical tidbit, it is time to dive into the actual database code. As I mentioned in the <a href="https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/" data-type="URL" data-id="https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/" target="_blank" rel="noreferrer noopener">previous article</a>, I chose to use <strong>Deta Space</strong> as the database provider. There are two reasons for this. The first is the ease of use and the second are its similarities to my favorite NoSQL database MongoDB. </p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: Please <a href="https://blog.finxter.com/how-i-created-a-weekly-shopping-list-and-recipe-app-in-streamlit/" data-type="post" data-id="1096859" target="_blank" rel="noreferrer noopener">check my article on creating a shopping list</a> in Streamlit on how to set it up. It takes only a few minutes.</p>



<p>For reference, the server directory with all the code to get the FastAPI server working looks as follows:</p>



<pre class="wp-block-preformatted"><code>server
├── db.py
├── main.py
├── models.py
├── requirements.txt
├── .env</code></pre>



<p>All the database code will live in the <code>db.py</code> file. For the Pydantic models, I’ll use <code>models.py</code>.</p>



<h2 class="wp-block-heading">Database Functions</h2>



<p>The database functions are roughly divided into three parts. </p>



<ul class="wp-block-list">
<li>We first need functionality for everything related to <em>users</em>. </li>



<li>Next, we need to code that handles everything related to adding and managing <em>friends</em>. </li>



<li>The third part applies to all the code for managing <em>thoughts</em>. Thoughts are Peerbrain’s equal of messages/tweets.</li>
</ul>



<p>The file will also contain some helper functions to aid with managing the public keys of users. I’ll go into a lot more detail on this in the article about encryption.</p>



<p>To set up our <code>db.py</code> file we first need to import everything needed. As before, I’ll show the entire list and then explain what everything does once we write code that uses it.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">"""This file will contain all the database logic for our server module. It will leverage the Deta Base NoSQL database api."""


from datetime import datetime
import math
from typing import Union
import os
import logging
from pprint import pprint #pylint: disable=unused-import
from uuid import uuid4
from deta import Deta
from dotenv import load_dotenv
from passlib.context import CryptContext</pre>



<p>The <code>#pylint</code> comment you can see above is used to make sure <code>pylint</code> skips this import. I use <code><a href="https://blog.finxter.com/a-simple-guide-to-the-pprint-module-in-python/" data-type="post" data-id="508127" target="_blank" rel="noreferrer noopener">pprint</a></code> for displaying dictionaries in a readable way when testing. As I don’t use it anywhere in the actual code, <code>pylint</code> would start to fuss otherwise.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Tip</strong>: <em>For those interested, </em><a href="https://pypi.org/project/pylint/" target="_blank" rel="noreferrer noopener"><em>pylint</em></a><em> is a great tool to check your code for consistency, errors and, code style. It is static, so it can’t detect errors occurring at runtime. I like it even so</em><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /><em>.</em></p>



<p>After having imported everything, I first initialize the database. The <code>load_dotenv()</code> below, first will load all my environment variables from the <code>.env</code> file.&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">load_dotenv()


#---DB INIT---#
DETA_KEY = os.getenv("DETA_KEY")
deta = Deta(DETA_KEY)
#---#
USERS = deta.Base("users")
THOUGHTS = deta.Base("thoughts")
KEYS = deta.Base("keys_db")
</pre>



<p>Once the variables are accessible, I can use the Deta API key to initialize Deta. Creating Bases in Deta is as easy as defining them with <code>deta.Base</code>. I can now call the variable names to perform CRUD operations when needed.</p>



<h2 class="wp-block-heading">Generate Password Hash</h2>



<p>The next part is very important. It will generate our password hash so the password is never readable. Even if someone has control of the database itself, they will not be able to use it. <code>Cryptcontext</code> itself is part of the <a href="https://passlib.readthedocs.io/en/stable/" target="_blank" rel="noreferrer noopener"><code>passlib</code> library</a>. This library can hash passwords in multiple ways.<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#---PW ENCRYPT INIT---#
pwd_context = CryptContext(schemes =["bcrypt"], deprecated="auto")
#---#
def gen_pw_hash(pw:str)->str:
    """Function that will use the CryptContext module to generate and return a hashed version of our password"""
   
    return pwd_context.hash(pw)</pre>



<h2 class="wp-block-heading">User Functions</h2>



<p>The first function of the user functions is the easiest. It uses Deta’s <code>fetch</code> method to retrieve all objects from a certain Base, <code>deta.users</code>, in our case.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#---USER FUNCTIONS---#
def get_users() -> dict:
    """Function to return all users from our database"""
    try:
        return {user["username"]: user for user in USERS.fetch().items}
    except Exception as e:
        # Log the error or handle it appropriately
        print(f"Error fetching users: {e}")
        return {}</pre>



<p>The fact that the function returns the found users as a dictionary makes them easy to use with FastAPI. As we contact a database in this function and all the others in this block, a <code>try</code>&#8211;<code>except</code> block is necessary.&nbsp;</p>



<p>The next two functions are doing the same thing but with different parameters. They accept either a <code>username</code> or an <code>email</code>. </p>



<p>I am aware that these two could be combined into a single function with an <code>if</code><em>-statement</em>. I still do prefer the two separate functions, as I find them easier to use. Another argument I will make is also that the email search function is primarily an end user function. I plan to use searching by username in the background as a helper function for other functionality.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_user_by_username(username:str)->Union[dict, None]:
    """Function that returns a User object if it is in the database. If not it returns a JSON object with the
    message no user exists for that username"""
   
    try:
        if (USERS.fetch({"username" : username}).items) == []:
            return {"Username" : "No user with username found"}
        else:
            return USERS.fetch({"username" : username}).items[0]
    except Exception as error_message:
        logging.exception(error_message)
        return None


def get_user_by_email(email:str)->Union[dict, None]:
    """Function that returns a User object if it is in the database. If not it returns a JSON object with the
    message no user exists for that email address"""
   
    try:
        if (USERS.fetch({"email" : email}).items) == []:
            return {"Email" : "No user with email found"}
        else:
            return USERS.fetch({"email" : email}).items[0]
    except Exception as error_message:
        logging.exception(error_message)
        return None
</pre>



<p>The functions above both take a parameter that they use to filter the <code>fetch</code> request to the Deta Base <code>users</code>. </p>



<p>If that filtering results in an <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-create-an-empty-list-in-python/" data-type="post" data-id="453870" target="_blank">empty list</a> a proper message is returned. If the returned list is not empty, we use the <code>.items</code> method on the fetch object and return the first item of that <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank">list</a>. In both cases, this will be the<em> </em><code>user</code> object that contains the query string (email or username).</p>



<p>The entire sequence is run inside a <em>try-except</em> block as we are trying to contact a database.</p>



<h2 class="wp-block-heading">Reset User Password</h2>



<p>When working with user creation and databases, a function to reset a user’s password is required. The next function will take care of that.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def change_password(username, pw_to_hash):
    """Function that takes a username and a password in plaintext. It will then hash that password>
    After that it creates a dictionary and tries to match the username to users in the database. If
    successful it overwrites the previous password hash. If not it returns a JSON message stating no
    user could be found for the username provided."""
   
    hashed_pw = gen_pw_hash(pw_to_hash)
    update= {"hashed_pw": hashed_pw }
   
    try:
        user = get_user_by_username(username)
        user_key = user["key"]
        if not username in get_users():
            return {"Username" : "Not Found"}
        else:
           
            return USERS.update(update, user_key), f"User {username} password changed!"
    except Exception as error_message:
        logging.exception(error_message)
        return None  </pre>



<p>This function will take a username and a new password. It will first hash that password and then <a href="https://blog.finxter.com/how-to-create-a-dictionary-from-two-lists/" data-type="post" data-id="316802" target="_blank" rel="noreferrer noopener">create a dictionary</a>. Updates to a Deta Base are always performed by calling the update method with a dictionary. As in the previous functions, we always check if the username in question exists before calling the update. Also, don’t forget the <em>try-except</em> block!</p>



<h2 class="wp-block-heading">Create User</h2>



<p>The last function is our most important one :-). You can’t perform any operations on user objects if you have no way to create them! Take a look below to check out how we’ll handle that.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def create_user(username:str, email:str, pw_to_hash:str)->None:
    """Function to create a new user. It takes three strings and inputs these into the new_user dictionary. The function then
    attempts to put this dictionary in the database"""


    new_user = {"username" : username,
                "key" : str(uuid4()),
                "hashed_pw" : gen_pw_hash(pw_to_hash),
                "email" : email,
                "friends" : [],
                "disabled" : False}
    try:
        return USERS.put(new_user)
    except Exception as error_message:
        logging.exception(error_message)
        return None
</pre>



<p>The user creation function will take a username, email, and password for now. It will probably become more complex in the future, but it serves our purposes for now. Like the Deta update method, creating a new item in the database requires a <a href="https://blog.finxter.com/python-dictionary/" data-type="post" data-id="5232" target="_blank" rel="noreferrer noopener">dictionary</a>. Some of the necessary attributes for the dictionary are generated inside the function. </p>



<p>The key needs to be unique, so we use Python’s <code>uuid4</code> module. The friend’s attribute will contain the usernames of other users but starts as an empty list. The disabled attribute, finally, is set to false.&nbsp;</p>



<p>After finishing the initialization, creating the object is a matter of calling the Deta <code>put </code>method. I hear some of you thinking that we don’t do any checks if the username or email already exists in the database. You are right, but I will perform these checks on the endpoint receiving the post request for user creation.</p>



<h2 class="wp-block-heading">Some Coding Thoughts and Learnings</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="894" height="590" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-84.png" alt="" class="wp-image-1189002" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-84.png 894w, https://blog.finxter.com/wp-content/uploads/2023/03/image-84-300x198.png 300w, https://blog.finxter.com/wp-content/uploads/2023/03/image-84-768x507.png 768w" sizes="(max-width: 894px) 100vw, 894px" /><figcaption class="wp-element-caption"><a href="https://github.com/shandralor/PeerBrain" data-type="URL" data-id="https://github.com/shandralor/PeerBrain" target="_blank" rel="noreferrer noopener">GitHub</a> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f448.png" alt="👈" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Join the open-source PeerBrain development community!</figcaption></figure>
</div>


<p>One thing that never ceases to amaze me is the amount of documentation I like to add. I do this first in the form of <strong>docstrings </strong>as it helps me keep track of what function does what. I find it boring most of the time, but in the end, it helps a lot! </p>



<p>The other part of documenting that I like is <strong>type hints</strong>. I admit they sometimes confuse me still, but I can see the merit they have when an application keeps growing.&nbsp;</p>



<p>We will handle the rest of the database function in the next article. See you there!</p>



<h2 class="wp-block-heading">Participate in Building the Decentralized Social Brain Network <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h2>



<p>As before, I state that I am completely self-taught. This means I’ll make mistakes. If you spot them, please <a href="https://discord.com/invite/Cn7jWhTckP" data-type="URL" data-id="https://discord.com/invite/Cn7jWhTckP" target="_blank" rel="noreferrer noopener">post them on Discord</a> so I can remedy them <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" />.&nbsp;</p>



<p>As always, feel free to ask me questions or pass suggestions! And check out the GitHub repository for participation!</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>GitHub</strong>: <a rel="noreferrer noopener" href="https://github.com/shandralor/PeerBrain" target="_blank">https://github.com/shandralor/PeerBrain</a></p>



<p></p>
<p>The post <a href="https://blog.finxter.com/my-journey-to-help-build-a-p2p-social-network-database-code-structure/">My Journey to Help Build a P2P Social Network &#8211; Database Code Structure</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>BrainWaves P2P Social Network &#8211; How I Created a Basic Server</title>
		<link>https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/</link>
		
		<dc:creator><![CDATA[Tom Teck]]></dc:creator>
		<pubDate>Sun, 26 Feb 2023 17:33:47 +0000</pubDate>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Distributed Systems]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[Peer to Peer Systems]]></category>
		<category><![CDATA[PeerBrain]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1163183</guid>

					<description><![CDATA[<p>Welcome back to the Brainwaves P2P project, or at least my take on it :-). The previous article was a theoretical explanation of how I envision this project. It is now time to start laying the groundwork! I learn as I go&#8230; As some of you might have guessed already, I’m a completely self-taught coder. ... <a title="BrainWaves P2P Social Network &#8211; How I Created a Basic Server" class="read-more" href="https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/" aria-label="Read more about BrainWaves P2P Social Network &#8211; How I Created a Basic Server">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/">BrainWaves P2P Social Network &#8211; How I Created a Basic Server</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Welcome back to the <a href="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/" data-type="post" data-id="1132824" target="_blank" rel="noreferrer noopener">Brainwaves P2P project</a>, or at least my take on it :-). </p>



<p>The <a href="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/" data-type="URL" data-id="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/" target="_blank" rel="noreferrer noopener">previous article</a> was a theoretical explanation of how I envision this project. It is now time to start laying the groundwork! </p>



<h2 class="wp-block-heading">I learn as I go&#8230;</h2>



<p>As some of you might have guessed already, I’m a completely self-taught coder. Because of that, I’m sure many professionals might not agree with how I code. </p>



<p>I accept that and will welcome any constructive criticism. I have been learning non-stop since I started this project. I assume this will not slow down anytime soon. YouTube is my main source of knowledge as I learn best when seeing something done. I am, in other words, a visual learner. I found an article that explains it well here. </p>



<p>Articles on various sites are the other half of how I learn new concepts in coding. That is how I found Finxter :-).</p>



<p>So to sum it up, my code is far from perfect, and I will never claim it is. This is my take on trying to solve this puzzle. I actually look forward to alternative approaches! </p>



<p>You can <a href="https://github.com/shandralor/PeerBrain" data-type="URL" data-id="https://github.com/shandralor/PeerBrain" target="_blank" rel="noreferrer noopener">open issues on my GitHub</a> if you want to address something.</p>



<p>Now that we all know where we stand let us dive right in! <strong>How to build a server for our peer-to-peer social network app?</strong></p>



<h2 class="wp-block-heading">Flask vs FastAPI</h2>



<p>In the <a href="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/" data-type="URL" data-id="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/" target="_blank" rel="noreferrer noopener">previous article</a>, I mentioned that I want to use FastAPI to build the relay server, as opposed to Flask. As I have done before and will do again, I asked ChatGPT about the differences between Flask and FastAPI.</p>



<pre class="wp-block-preformatted"><strong><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f916.png" alt="🤖" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Flask vs FastAPI</strong>

<em>Flask is based on the Werkzeug WSGI (Web Server Gateway Interface) toolkit, which is synchronous by default. However, Flask can still be used to build asynchronous applications. You will need to use a third-party library like gevent or asyncio. With these libraries, Flask can use coroutines and event loops to handle I/O operations asynchronously.</em>

<em>FastAPI, on the other hand, is designed to be fully asynchronous from the ground up. It uses the async/await syntax of Python to write asynchronous code. It is based on the ASGI (Asynchronous Server Gateway Interface) specification. FastAPI uses the Starlette framework as its foundation. the framework provides a high-performance event loop and asynchronous request handlers.</em></pre>



<p>Both the speed and the asynchrony determined my choice for FastAPI. </p>



<p>Those of you familiar with Flask will know about its built-in development server. As FastAPI doesn’t have this, we’ll need to install a separate server.</p>



<h2 class="wp-block-heading">Uvicorn Server </h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://www.uvicorn.org/" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="747" height="468" src="https://blog.finxter.com/wp-content/uploads/2023/02/image-350.png" alt="" class="wp-image-1163291" srcset="https://blog.finxter.com/wp-content/uploads/2023/02/image-350.png 747w, https://blog.finxter.com/wp-content/uploads/2023/02/image-350-300x188.png 300w" sizes="auto, (max-width: 747px) 100vw, 747px" /></a></figure>
</div>


<p>This is where I encountered my first small hiccup. I code on Windows (I know, sue me <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f61d.png" alt="😝" class="wp-smiley" style="height: 1em; max-height: 1em;" />), and I wanted to use Uvicorn. As this only runs on Linux, I needed to get it to function in WSL. </p>



<p>I’ll not go into all the details here, but I could write something about it if anyone has an interest in it. Let me know!</p>



<p>After getting Uvicorn to function as it should, we can continue. It is important to remember that the Python interpreter on WSL does not share anything with its Windows counterpart. This means that you either need two separate virtual environments or that you install pip packages for each OS.</p>



<h2 class="wp-block-heading">Creating Basic FastAPI App</h2>



<p>Once all this annoying prep work is done, creating a basic FastAPI app is very easy. We first import FastAPI as below:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from fastapi import FastAPI</pre>



<p>All you need to do afterward is define the basic app and create an endpoint.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#---APP INIT---#
app = FastAPI()
 
@app.get("/")
async def root():
    return {"message": "Hello World"}</pre>



<p>To get this to run, you need to navigate to the working directory of your FastAPI project via WSL. Afterward, you call the Uvicorn server. The command below assumes you called your Python file <code>main.py</code>!</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">uvicorn main:app --reload</pre>



<p>I usually run the Uvicorn server in a separate terminal instance of WSL. </p>



<p>That way, I can leave it on and test any changes I make immediately. Later, when I’ll be working on the client also, I can split the terminal. You can then make API calls through the client terminal window. FastAPI’s response in the server WSL window is then visible immediately.</p>



<h2 class="wp-block-heading">Receiving &#8220;Hello World&#8221; from Server</h2>



<p>If you now navigate to <code>127.0.0.1:8000</code> you should get a JSON response with the <code>"Hello World"</code> we returned in the endpoint above. We will change this endpoints function later, but for now, it works to prove our API is working.</p>



<p>For the API server, I have the following layout in mind. It might change throughout the development process. I currently foresee two endpoints that do not require the user to be logged in with a JWT token. The first will be to get that token, and the second to register a new user. Everything else will require the user to be authenticated.</p>



<p>I stated earlier that I would change the root’s endpoint function. Its new role is now to allow a user to request a JWT token. The token is only granted after providing a correct combination of username and password. This requires a dedicated set of both helper functions and Pydantic models to work. </p>



<p>I will go into this in another article, as it requires much explaining :-). It was something I am still learning myself.</p>



<h2 class="wp-block-heading">Endpoint Layout</h2>



<p>The current layout of my endpoints at a high level is the following:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#---OPEN ENDPOINTS---#


#Root route to get token
@app.post("/", response_model=Token)


#Route to create a new user
@app.post("/api/v1/users")


#---AUTH ENDPOINTS---#


#Route to get all current users and their attributes(development only)
@app.get("/api/v1/users")


#Route to test if the token is valid, used while authenticating
@app.get("/api/v1/token-test")


#Route to get all thoughts/messages created by a certain user
@app.get("/api/v1/thoughts/{username}")


#Route to return all thoughts/messages containing the query string
@app.get("/api/v1/thoughts/{query_str}")


#Route to create a new message/thought
@app.post("/api/v1/thoughts")


#Route to return all info about the current user(like a user profile)
@app.get("/api/v1/me", response_model=User)
</pre>



<p>The current setup should allow for the barebones functionality of the application. At least from a server point of view. The routes above and/or their function are liable to change during development.&nbsp; I do find it helps to have a visual reminder of what I am working toward. That is why I created the high-level outlay. As you might recall, I am a visual learner <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h2 class="wp-block-heading">Database Considerations</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="709" height="704" src="https://blog.finxter.com/wp-content/uploads/2023/02/image-351.png" alt="" class="wp-image-1163293" srcset="https://blog.finxter.com/wp-content/uploads/2023/02/image-351.png 709w, https://blog.finxter.com/wp-content/uploads/2023/02/image-351-300x298.png 300w, https://blog.finxter.com/wp-content/uploads/2023/02/image-351-150x150.png 150w" sizes="auto, (max-width: 709px) 100vw, 709px" /></figure>
</div>


<p>I will dedicate the last part of this article to the database part. As we need to store both users, user credentials, and messages/tweets somewhere, a database is a must.</p>



<p>If you have read any of my previous articles, you will know I like Deta a lot.</p>



<p>Their NoSQL databases work great for development. They recently evolved into <a href="https://deta.space/manual/apps">Deta Space</a>. This change makes their ecosystem even more interesting for developers. The fact that they are free is also important for a single developer coding this app on his own time <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f61d.png" alt="😝" class="wp-smiley" style="height: 1em; max-height: 1em;" />. Make sure to check them out!</p>



<p>The next article will focus on both the database code and the Pydantic models we will need to get our API to function.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Next Article</strong>: <a href="https://blog.finxter.com/my-journey-to-help-build-a-p2p-social-network-database-code-structure/" data-type="post" data-id="1188982" target="_blank" rel="noreferrer noopener">My Journey to Help Build a P2P Social Network – Database Code Structure</a></p>



<h2 class="wp-block-heading">Join GitHub</h2>



<p>As always, feel free to ask me questions or pass suggestions! And check out the GitHub repository for participation!</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>GitHub</strong>: <a rel="noreferrer noopener" href="https://github.com/shandralor/PeerBrain" target="_blank">https://github.com/shandralor/PeerBrain</a></p>



<p></p>
<p>The post <a href="https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/">BrainWaves P2P Social Network &#8211; How I Created a Basic Server</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>The P2P Bootstrapping Problem &#8211; Starting My Journey To Help Build a P2P Social Network</title>
		<link>https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/</link>
		
		<dc:creator><![CDATA[Tom Teck]]></dc:creator>
		<pubDate>Tue, 14 Feb 2023 15:25:26 +0000</pubDate>
				<category><![CDATA[Distributed Systems]]></category>
		<category><![CDATA[Peer to Peer Systems]]></category>
		<category><![CDATA[PeerBrain]]></category>
		<category><![CDATA[Projects]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1132824</guid>

					<description><![CDATA[<p>Decentralized P2P Social Nets When I saw the Finxter announcement about trying to create a decentralized P2P social network, I knew I had to take part. My interest peaked as it had seldom been before. I love the idea of a social network that can function without interference from companies or governments. It feels like ... <a title="The P2P Bootstrapping Problem &#8211; Starting My Journey To Help Build a P2P Social Network" class="read-more" href="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/" aria-label="Read more about The P2P Bootstrapping Problem &#8211; Starting My Journey To Help Build a P2P Social Network">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/">The P2P Bootstrapping Problem &#8211; Starting My Journey To Help Build a P2P Social Network</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Decentralized P2P Social Nets</h2>



<p>When I saw the Finxter announcement about trying to create a <a href="https://github.com/shandralor/PeerBrain" data-type="URL" data-id="https://github.com/shandralor/PeerBrain" target="_blank" rel="noreferrer noopener">decentralized P2P social network</a>, I knew I had to take part. </p>



<p>My interest peaked as it had seldom been before. I love the idea of a social network that can function without interference from companies or governments. It feels like something the world needs. </p>



<p>On why someone did not build it yet, I don’t have a definitive answer. The only thing that occurs to me is that there is no real demand for it (yet). There might actually be some active pushback from major players in the field. </p>



<h2 class="wp-block-heading">From Matrix to FastAPI</h2>



<p>From a financial and monetization point of view, such an application is not very interesting. It would be very difficult to push ads on people as there would be no central control over the network. Which suits me fine :-).</p>



<p>To be honest, there are some standards that are very well developed and so far along that they are production worthy. I’m thinking of the <a href="https://matrix.org/docs/api/#overview" data-type="URL" data-id="https://matrix.org/docs/api/#overview" target="_blank" rel="noreferrer noopener">Matrix API</a>. </p>



<p>The problem, at least as far as I am concerned, is that it is difficult to get working. I spent a couple of hours trying to get through the documentation. In the end, I was not sure I understood it better than at the start. So complexity is a definite issue; I’ll have to keep that in mind.</p>



<p>After some emails going back and forth between Chris and myself, we arrived at the following conclusion. </p>



<p>The basic idea for an initial version of the app is a web-based, decentralized tweeting application. So far, so good. Now I needed to do (a lot of ) brainstorming on how to set this up. </p>



<p>As you might have gleaned from <a href="https://blog.finxter.com/author/tomteck0809847204978284/" data-type="URL" data-id="https://blog.finxter.com/author/tomteck0809847204978284/" target="_blank" rel="noreferrer noopener">my previous articles</a>, Python is my language of choice. I like HTML and CSS too, but not at the same level. So I continued my search for something that could both supply the functionality needed and still allow me to use Python. </p>



<p>The result of that search is the FastAPI framework.</p>



<p>This framework immediately piqued my interest immediately. I’ve been delving ever deeper into its uses, and I am convinced it will work well. The more I learn, the more I am reassured this could be the reliable server-side for the P2P social network we need! </p>



<h2 class="wp-block-heading">The Role of Servers in P2P Systems</h2>



<p>The next question now is: <em>how I can go about doing that?</em> </p>



<p>I discussed this with Chris some more. He posed the justified question of why we need a server for a P2P application.</p>



<p>The problem with P2P at the moment is that we are all still using IPv4. The problem with IPv4 is that we only have a limited amount of addresses available. To address this, there are certain workarounds used everywhere.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://bford.info/pub/net/p2pnat/" data-type="URL" data-id="https://bford.info/pub/net/p2pnat/" target="_blank" rel="noreferrer noopener">Peer-to-Peer Communication Across Network Address Translators</a></p>



<p>NATs and rotating public IPs are the default way of “solving” this problem of scarcity. NATs and their private IP address ranges do pose problems. </p>



<p>When clients want to connect to each other, they need to traverse those NATs. Connecting to the outside from within the NAT is not a problem. Finding someone else behind their NAT is something else. </p>



<p>Added to this comes the fact that providers also rotate IP addresses. So, in the end, you are never certain where to find someone exactly <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" />. </p>



<h2 class="wp-block-heading">Relay Servers in P2P Systems</h2>



<p>This is where the “relay” server becomes important. It can be hosted on a server with a static IP, which means it will stay in one place, so to speak. By adding the option to our application to enter an IP or domain name, we will be able to connect to it. Once the connection is active and client details exchanged, we could, in theory, cut out the server. </p>



<p>Direct contact between the two clients would then become possible. It is an approach I will look into more in the future. But for now, I find it to still have too many uncertainties to get it to function reliably. A great article about this can be found <a href="https://bford.info/pub/net/p2pnat/" data-type="URL" data-id="https://bford.info/pub/net/p2pnat/" target="_blank" rel="noreferrer noopener">here</a>.</p>



<p>So, how to solve this problem? </p>



<p>Let us look back to what we wanted to build. The purpose of this project in its first iteration is the following. We want some version of a <em>“decentralized P2P application that would allow some form of tweeting”</em>. </p>



<h2 class="wp-block-heading">A Word on Torrents</h2>



<p>When I hear P2P, I can’t help but think of torrents. A vital part of torrents is the tracker. According to our good friend ChatGPT, a torrent tracker is:</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4ac.png" alt="💬" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Definition</strong>: <em>A torrent tracker is a server that helps in the communication between peers using the BitTorrent file sharing protocol. It keeps track of the peers participating in a specific torrent &#8220;swarm&#8221; (a group of peers sharing a particular file. This facilitates the distribution of the file and allows peers to find each other.</em></p>



<p>It&#8217;s important to note that torrent trackers do not host any files themselves. Nor do they guarantee the availability of a particular file. They ease communication between peers and provide information about the location of seeds and peers.</p>



<p>This is the point where I am of two minds. </p>



<p>Implementing the application as a true P2P torrent equivalent could certainly work well. It would, however depend on the app running constantly. This is because it needs to serve its “tweets” or messages when requested by other interested clients. It would also need a constant network connection.</p>



<p>The other idea I am playing with is finding a way that would allow for the storing of messages on the relay server. </p>



<p>They would need to be encrypted, so it would be impossible for someone who had or gained control of the server to read them. </p>



<h2 class="wp-block-heading">Encrypting Messages</h2>



<p>For this to work, a cryptographic key must be exchanged when you “befriend” someone on the system. This key would allow for the decoding of those messages when you request them from the server. After decoding, you could then read them.</p>



<p>To me, the second approach feels more reliable to put in place. Especially when considering that it will allow for a proof of concept to be developed easier and faster. </p>



<p>Reliability is a big factor in that. Having decided this, I know what I have to do! </p>



<h2 class="wp-block-heading">Stay Tuned</h2>



<p>The second article in this series will focus on coding out both the basic server and CLI client. See you soon!</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <a rel="noreferrer noopener" href="https://github.com/shandralor/PeerBrain" data-type="URL" data-id="https://github.com/shandralor/PeerBrain" target="_blank">Join the decentralized P2P project on GitHub!</a></p>



<p>You can also check out the next tutorial on the Finxter blog about how I chose a server for the P2P project. </p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/brainwaves-p2p-social-network-how-i-created-a-basic-server/" data-type="post" data-id="1163183" target="_blank">BrainWaves P2P Social Network – How I Created a Basic Server</a></p>
<p>The post <a href="https://blog.finxter.com/how-i-started-my-journey-to-help-build-a-p2p-social-network-part-1-deciding-on-a-framework/">The P2P Bootstrapping Problem &#8211; Starting My Journey To Help Build a P2P Social Network</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.finxter.com @ 2026-04-07 01:16:45 by W3 Total Cache
-->