VOOZH about

URL: https://thenewstack.io/build-your-own-decentralized-twitter-part-3-hello-mastodon/

⇱ Build Your Own Decentralized Twitter, Part 3: Hello Mastodon - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-12-10 08:00:21
Build Your Own Decentralized Twitter, Part 3: Hello Mastodon
tutorial,
Frontend Development / Open Source / Software Development

Build Your Own Decentralized Twitter, Part 3: Hello Mastodon

David Eastman continues his tutorial series on developing a decentralized Twitter. This week, he examines Mastodon's federated approach.
Dec 10th, 2022 8:00am by David Eastman
👁 Featued image for: Build Your Own Decentralized Twitter, Part 3: Hello Mastodon
Image via Shutterstock 

In Part 1 of this series, we created an architecture for a distributed social media system, using a Visual Studio code project and corresponding JSON files. In Part 2, we figured out how to do mitigations — addressing problems like deleted tweets.

We saw in the previous post that our distributed project system was user-centered but very unruly. What we need to do now, in Part 3, is trade a little control for a lot more certainty. That means looking into Mastodon’s federated social media approach.

In a federated model, servers talk to each other to build up the social graph. Thus a Mastodon server is simply a server that complies with the appropriate protocols and takes its part in a federation. A user joins a server, and in turn takes part in that federation. These servers have their own reasons to exist, but typically it’s to represent a particular community’s concerns.

We can see that the federation model is a little like our project model, but with the tweet stores sitting with one of the tweet view servers. From the server’s point of view, there are now local tweets and outer tweets. Let’s see how federated servers improve on the weaknesses of our projects fully “user-centered” model.

Identity

In our project, there was no useful way of uniquely identifying anything (tweeter or tweet viewer), but that was part and parcel of a “tweeter-centered” system. While an identity is not as absolute as it is on Twitter, federated servers do have identities. Although Mastodon claims to have no central authority, servers are unique in the sense of their internet domain’s uniqueness. This means that a user on Mastodon has an identity, because a user is associated with one server. My address might well be @eastmad@mastodon.social (I don’t remember what it actually is), and this has a good chance of being both unique and secure. I note there is a “Mastodon Server Covenant,” which is an inevitable attempt to have some central guidance above the possible chaos. The bottom line is that by giving some trust to the server, we gain some advantages.

Structure

In our model, access to a tweeter’s tweets was controlled by the tweeter, so conversations could disappear at any time. With a federation, servers communicate amongst themselves to build the graph, so the graph is not at the whim of individuals in quite the same way. Mastodon servers often map to real-world communities, like Mastodon.cat for Catalan speakers, so users might have a pre-existing commonality. While iterating through the graph is somewhat costly, as we’ll see later, the server is in control of a valid source of truth. So unlike with our project, the Mastodon model doesn’t need to consider mitigations to maintain stability.

Content

We saw in our project model that because the tweeter controlled their content (and thus was the source of truth), not only could you tweet anything, you could also edit anything at any time. With a federated model, the content is owned by the server. Servers independently decide what their policies are. Unsurprisingly, most do what they can to ban hate speech, for example.

Freedom of expression does survive federation, but that freedom is Balkanized. So you can indeed join a server that allows you to express your deeply unpopular thoughts, but you may find that your foul conversations only exist on a very small branch of the social graph because other servers don’t talk to your server. This emphasizes the “social bubble” or “safe space”, which may at different times be seen as a boon or a drawback.

Here Comes the Mastodon

If you followed along with the project code, you will be ready to see how the somewhat more comprehensive code behind Mastodon deals with conversation. How would we alter our model to work with the Mastodon API?

Our project only threaded messages in a simplistic way, because it had no idea about ancestors and descendants. It just did a quick check on the sorted list before displaying a tweet, if the last message id had the same id as the reply-to field in the current message:

long prevtweetid = -1;
char sepchar = ':';
foreach (var tweetfrom in totalTweets)
{
 if (prevtweetid == tweetfrom.tweet.Replyto)
 sepchar = '↳';
 else sepchar = '-';

 Console.WriteLine($".. {sepchar} {tweetfrom.tweet.Text} ");

 prevtweetid = tweetfrom.tweet.Time;
}

Clearly, a post could be a reply to any previous post.

For full functionality, we have to know — for each post — which is the parent post (there can be only one, or none), and how many children it has (none or many).

In Mastodon, a post is called a status. Remember, when Twitter started in 2006, it invited intrepid pioneers to respond to the statement “What Are You Doing?”, because at that point it wasn’t designed to be used for conversation: it was more of a diary status.

Each status has an id. As in our project, the id is probably based on a Unix timestamp. And just like in our project, there is a in_reply_to_id that stores the id of the status it is replying to. To simply fetch a status with the API, you use the status id:

/api/v1/statuses/1

We could replicate this easily enough in our project, but we would need a user. Unlike in a truly distributed system, in a federated system the tweet (or status) is central — not the user.

Mastodon’s API calls a conversation a context. (This is what happens when you let developers name things!) Let’s take our existing example conversation with Alan, Beth and Cath, and think of it in the Mastodon model:

👁 Image

Alan’s first status would have an id of 1668435369. To get at the conversation, the API call would be:

/api/v1/statuses/1668435369/context

Alan started the conversation, so the status has no ancestors and only one descendant. If we called the API on the one descendant (1668435540), we would discover the single descendant (1668438963), etc.

Mastodon context calls only return these two lists, ancestor ids and descendant ids, but after a few calls our entire conversation would be “parsed” — and it would do a much more comprehensive job of modelling the conversation than our project.

You should already see that if you wanted to investigate a complicated conversation, you would immediately become a tree surgeon, having to go up and down branches to count all the leaves. Eventually, you come up to the trunk, find all the major branches, and can at least be sure you have the full scope.

As you can see from this graph, wherever you started you would need a good number of Mastodon context API calls (about nine) to model the whole tree with only 11 nodes:

👁 Image

The Twitter Kingdom vs. the Federation

Ultimately, the two contenders for the “public conversation” social media crown would appear to be the monolith Twitter and the decentralized Mastodon. Hopefully, I’ve shown that they are quite dissimilar, with the Twitter Kingdom presenting an entire empire in a box, while the federation can grow or shrink based on the real-world communities that want to take part.

As we’ve seen recently, a Kingdom is dependent on a wise ruler — and suffers without one. A federation nurtures healthy communities much more than the lone individual. Both ideas are valid, and in my view, it is more appropriate to celebrate the good features of both rather than wanting only one to dominate.

TRENDING STORIES
David has been a London-based professional software developer with Oracle Corp. and British Telecom, and a consultant helping teams work in a more agile fashion. He wrote a book on UI design and has been writing technical articles ever since....
Read more from David Eastman
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.