Decentralized social media

Welcome to the Fediverse

Article from Issue 227/2019

If you're looking for social media options where the user has more control, you'll find a range of options to explore in the Fediverse, including the popular Mastodon.

Despite what it may seem, despite its promise of unbridled communication possibilities and its supposed gift of giving voice to the traditionally voiceless, current social media is a walled garden at best, although a slimy cesspit with bars over the top would be a more apt description.

The problem with the current social media status quo is that the platform does not have your interests at heart. The companies that run proprietary social media platforms gradually introduce more and more restrictive terms of service, package your personal data and sell it off to other companies and governments, make their algorithms more manipulative, and so on.

In the Beginning

You could renounce social media altogether and go and live as hermit, but why throw the baby out with the bath water? The Fediverse [1] is the FOSS community's response to walled-gardened, personal data-leaching, closed, and proprietary social media. Strictly speaking, the Fediverse would cover all social media services (and some services that are not related to social media at all) that do not rely on a centralized and controlling entity (like, say, Facebook, Twitter, or Google), but instead allow a user to run their own server (often called a "pod") and to connect it to a network of similar servers to share the media between them. This allows user A to post to pod X and user B to read it from pod Y.

The genesis of the Fediverse can be traced back to projects like diaspora* [2] or [3], the earlier attempts to create a non-centralized network of social media. Things evolved and,'s creator, Evan Prodromou, developed [4], a framework upon which developers can build social media services.

It then came to pass that the W3C saw that was good. It took its essence and breathed new life into it, transforming it into ActivityPub [5] and publishing a recommendation.

The modern Fediverse was born. Let there be posts!

ActivityPub is a protocol and an API that allows the services that follow it to communicate across the Fediverse with nodes of the same kind of service or even with nodes of different services. For example, you can post a picture to your account on Pixelfed [6], a photo-sharing service (Figure 1), and have it pop up on the feeds of people who follow you on Mastodon [7], the Fediverse's most popular microblogging service (Figure 2).

Figure 1: What you post to Pixelfed…
Figure 2: … will pop up in the feeds of the people who follow you on Mastodon.

The Fediverse not only shares posts, but it also shares resources among nodes and even, by using P2P technologies, among clients. Say you decide to open your own PeerTube pod on your own server. Sure, you will need a decent amount of bandwidth, but not as much as you think, as the video service has clients watching the same clip share the load.

Apart from Mastodon, PeerTube, and Pixelfed, there is a Fediverse social media platform for audio aimed at musicians and podcasters called Funkwhale [8], and then there is Hubzilla [9], which describes itself as "a powerful platform for creating interconnected websites featuring a decentralized identity, communications, and permissions framework built using common web server technology" – so a federated Facebook? Hubzilla is a bit rough around the edges, and I haven't totally figured it out yet.

To be fair, most of the Fediverse is still rough around the edges. Pixelfed lacks a mobile phone app with camera integration, which, if I understand things right, would make it more attractive to Instagrammers. There does not seem to be a way of seeing who your subscribers are on PeerTube, and Hubzilla's interface looks dated and only implements ActivityPub as a plugin, so I'm guessing that connectivity with other services throughout its network may be spotty.

But this is kind of normal: Many of the Fediverse's services are in a nascent phase, with few users and nearly no posts.

This is not the case of Mastodon.

Mastodon Basics

Mastodon has been the subject of many articles predicting its demise, a nearly certain sign it will survive. Indeed, according to Mastodon's account-counting bot, Mastodon is quickly approaching 3.5 million users and is arguably the most popular, non-proprietary social media platform out there.

One of the reasons for its popularity may be due to its design – Mastodon just looks good (Figure 2 again). It is also simple to set up and use.

Mastodon is laid out in columns. The first column from the left is where you write your toots (up to 512 characters long). You can also add attachments: up to four pictures, or a video, or an audio file. You can also create a poll, decide on the privacy of the post (public, for followers only, direct message, etc.) and set the content as hidden behind a warning, which is ideal for spoilers or for potentially offensive material.

The second column shows your feed. This contains your own toots and those of the people you follow. The third column shows notifications, that is, when people favorite or boost your toots, direct messages they send you, comments, and so on.

The fourth column is a bit of a wildcard. By default, it shows a menu that allows you to pick between showing the local timeline (i.e., all the toots sent in your pod as they happen), the federated timeline, or all the toots posted in your pods network as they happen. You can also turn it into an inbox and show direct messages here, list the toots you have favorited over time, and make lists in which to classify the accounts you follow. Or if you click on a user's profile, it shows the user's information. Their latest stats and toots will also show up here.

You can also change what is shown in the fourth column using the toolbar across the top of the first column. The Getting Started button will take you back to the fourth column's original menu, the Local Timeline and Federated Timeline buttons will show each of those timelines in the fourth column.

The Settings button lets you configure everything regarding your account, from your display name and the look and feel of you account, to the custom applications you can use to mine information from Mastodon (more about this later).

In the Settings, by the way, you can also switch on a feature that lets you add more columns to your Mastodon interface. Go to Settings | Preferences and check the Enable Advanced Web Interface checkbox. The description says:

"If you want to make use of your entire screen width, the advanced web interface allows you to configure many different columns to see as much information at the same time as you want: Home, notifications, federated timeline, any number of lists, and hashtags."

Annoyingly, it does not say how to do this, so here it is: Say you want to add a new column with the federated timeline. Go to the fourth column, click on Federated Timeline, and the federated timeline will appear, occluding the Getting Started panel/menu/column/thingy. At the top right of the timeline, there is a configuration button. It looks like three little sliders. Click it and then click on + Pin in the fold out that appears. The column is now fixed and the Getting Started panel/menu/column/thingy will pop up to the right, allowing you to create more columns.

If you are interested in a trending topic, click on its hashtag and a feed with all the toots containing that hashtag will appear over the Getting Started column. Again, by clicking the configuration button in the top right of the column, you can pin the column and follow the topic as it develops (Figure 3).

Figure 3: Adding new columns lets you follow several timelines and hashtagged posts at the same time.

You can search for hashtags (and users and more things) using the search box located in the first column on the left, right beneath the toolbar.

Coding for Mastodon

With the rise of Mastodon's popularity, a few clients have popped up that allow you to access you account from your mobile. My personal preference leans towards Fedilab [10], which lets you manage accounts from Mastodon, PeerTube and soon Pixelfed, all from one single app (Figure 4).

Figure 4: Fedilab lets you manage multiple Mastodon and PeerTube accounts from your phone. It will soon support Pixelfed, too.

If you can't find a client you like, don't despair: Coding for Mastodon is easy. Its API is well-documented, and there are plenty of hooks in several programming languages.

Let's use Python to do some simple reading of data from accounts.

The module you need is called [11], and the documentation is located at [12].

You can install the modules with

sudo pip install

Let's write a short script that will solve a real problem: Mastodon's web stops showing the precise number of followers from 1,000 followers up, opting for showing, for example 2.7K, instead of, say, 2,734, 2,755, or whatever. This makes tracking the popularity of an account difficult.

Your first app uses the identification number of a toot to print out the name of the account and the number of its followers.

Before you even start to write your code, you must give your app a name and register it with the federated network of Mastodon pods you are going to query. To do this, log into your Mastodon account. Go to Settings | Development and click the New Application button. Fill in the Application Name text box and choose the scope of your application. In this case, you only have to leave the Read checkbox marked, because that is all you are going to do: Read data from the network.

Click Submit at the bottom of the page, and this will log your application with the federated network.

Now, if you click on your application's name, at the top of the page it will show a list of three keys. The key labeled Client Secret is the one you need to finish registering your application with the network (Figure 5).

Figure 5: Copy and paste the secret key into your application to register it with the network.

To finalize the registration, create your application with the code shown in Listing 1.

Listing 1 (Part One)

01 from mastodon import Mastodon
03 Mastodon.create_app(
04      'accountfollowers',
05      api_base_url = '[URL OF MASTODON          INSTANCE]',
06      to_file = '[CLIENT SECRET]'
07 )

On line 5, you put the URL of the Mastodon instance where you are registering your application. It will look something like:

api_base_url = ''

On line 6 you put the secret client key you saw above. The line will end up looking like this:

to_file = 'HHssuwd_AhTot4LlyMaDEUpK3y_aEJHjjIWJj2289usQsjSJqiw-d'

You run this version of your app once and then you can comment out or delete the registration code.

The final version of your application reads a list of toot URLs (see the box "Grabbing a Toot") from the command line and then processes them to figure out the account from whence they came and the number of followers that account has. The application would look like what you can see in Listing 2.

Grabbing a Toot

To grab the URL of an individual toot, click on the time the toot was published. This is in the upper right-hand corner of the toots box. The toot opens in a new window/tab with a URL that will look something like: 102552017830926864

Breaking it down, is the Mastodon instance (pod) hosting the toot, @davidrevoy is the user, and 102552017830926864 is the unique ID assigned to the toot.

Listing 2 (Part Two)

01 import sys
02 from mastodon import Mastodon
04 sys.argv.pop(0)
05 for eachToot in sys.argv:
06     tinstance = eachToot.split("/")[2]
07     taccount = eachToot.split("/")[3]
08     tid = eachToot.split("/")[4]
10     mastodon = Mastodon (
11         client_id = '[CLIENT SECRET]',
12         api_base_url = 'https://' + tinstance
13     )
15     print ("User:" + taccount)
16     print ("ID: " + str(mastodon.status(tid)['account']['id']))
17     print ("Followers: " + str(mastodon.status(tid)['account']['followers_count']))
18     print ("===")

This works is as follows:

  1. On lines 1 and 2, you import your modules: sys to be able to read parameters off of the command line, and mastodon for all the Mastodon stuff.
  2. On line 4, you remove the first element from the list of parameters passed from the command line, as it is the name of the script, and all you want are the links.
  3. On line 5, you loop through the links.
  4. On lines 6 through 8, you split the URL for each toot up into the Mastodon pod (instance) it was posted to, the name of the account, and the ID of the toot itself. If you have a URL for toot that looks like this:

 tinstance will contain, taccount will contain @kde and tid will contain 102455343978152552.

  1. On lines 10 through 13, you create a mastodon object that will let your application connect to the instance and identify itself with the secret client code.
  2. On line 15, you print the user name to the console.
  3. On line 16 and 17, you use's status() function to read data from the Mastodon instance.

The status() [13] function reads a toot (known in ActivityPub jargon as a status) given an id for the toot. It returns a toot dictionary [14], containing everything you need to know about the toot. The toot dictionary includes a subdictionary called account [15], which contains information about the account the toot came from, including the accounts id number (accessed in line 16) and the follower_count (accessed in line 17).

You can call your script like this:


Both the toot and account dictionaries contain much more information. You can also use them to pull the content of the toot or maybe make a list of accounts the user follows. The point is, that is it. It is that simple to interact with Mastodon from scripts.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Social Skills

    Creating a custom application that toots text to Mastodon (the Fediverse's version of Twitter) is simple and straightforward. But we can mix it up by adding images and video, scheduling posts, and changing privacy settings.

  • Mastodon

    Get started with free microblogging, interact with others who share you interests, and even help expand the Fediverse with your own Mastodon instance.

  • Mastodon Clients Post Line

    The open and simple Mastodon API makes it easy to create applications to interact with this federated microblogging platform. Here are some of the clients that the community has come up with and how you can use them.

  • toot

    If you are looking for a Twitter alternative, toot lets you interact with Mastodon from the command line.

  • Status Quo

    Creating your own clients to interact with your friends in the Fediverse is easy. A bit of Python and an off-the-shelf module will do the trick.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More