Navigate

Earthstar

Playground

You can play with the Earthstar API right here in your browser.

Open your browser's development console and start typing in Earthstar. It's been added to the global scope. You can find full documentation for the API here.

Let's begin by creating an identity. We'll use this to write some data shortly.

let identity = await Earthstar.Crypto.generateAuthorKeypair("suzy");

Feel free to change the shortname to whatever you want, as long as it's four lowercase letters or numbers.

We want to write some data, so let's make a replica. A replica is a local copy of a share's data which can synchronise with other replicas.

We should decide on a cool name for our share. You can change it to whatever you like, just make sure it starts with a + and that the part after the dot begins with a letter. Lowercase letters and numbers only.

let shareAddress = "+pizza2nite.p34axr";

With that settled, we need a storage driver to tell our replica how to save data. As we're just playing around in a console, let's persist everything to memory for now.

let driver = new Earthstar.ReplicaDriverMemory(shareAddress);

Now to instantiate our replica with the driver we just made:

let replica = new Earthstar.Replica(shareAddress, Earthstar.FormatValidatorEs4, driver);

Our replica is ready to go. Let's put some data in it with the identity we made earlier.

replica.set(identity, { content: 'Pizza Margherita!', path: '/my-order.txt', format: 'es.4'});

Let's check if it worked.

await replica.getLatestDocs();
[
	{
		_localIndex: 0,
		author: "@suzy.bklqpp6wuzv4t4qynjqvd2o7gaefk4776cb67fwo34xu6jfgwyaza",
		content: "Hello!",
		contentHash: "bgngqc33vltlnywgfhkdoda4if6hmct2s7mctiwehzcs63vbmq63q",
		deleteAfter: null,
		format: "es.4",
		path: "/greetings.txt",
		signature: "bcjpcgxxo7o5setztnoo4mgemlhh7ixloz6tnrdm24xf3gy62nbiafszdjpsdlildyqd6cp5gp46jxb4nummkxjmki4whnlgcjew4caa",
		timestamp: 1643809349409000,
		workspace: "+myshare.a123"
	}
]

Great! We should synchronise this to our friends so they know which pizza to order for us. First we'll need to put our replica inside a Peer, which is used to manage shares and coordinate with syncers.

let myPeer = new Earthstar.Peer();

And we'll add our replica to it.

myPeer.addReplica(replica);

Now we have a peer, we can sync it with other peers! We can do this with peers remotely over the network, but for now let's just pretend and sync with a local one.

let otherDriver = new Earthstar.ReplicaDriverMemory(shareAddress);
let otherReplica = new Earthstar.Replica(shareAddress, Earthstar.FormatValidatorEs4, otherDriver);

If we run otherReplica.getLatestDocs, we'll get an empty array. It doesn't have any documents in it yet. Let's put this replica in another peer so we can change that!

let otherPeer = new Earthstar.Peer();
otherPeer.addReplica(otherReplica);

Now that we have our two peers, let's sync them:

myPeer.sync(otherPeer);

And now if we ask for documents:

await otherReplica.getLatestDocs();

You'll see the document we made from before has synced over.