LevelDB & Crypto

WebRTC Swarm with Hyperlogs

LevelDB & Crypto

Check out a free preview of the full LevelDB & Crypto course

The "WebRTC Swarm with Hyperlogs" Lesson is part of the full, LevelDB & Crypto course featured in this preview video. Here's what you'd learn in this lesson:

Create a swarm of P2P connections using WebRTC and a signalhub.


Transcript from the "WebRTC Swarm with Hyperlogs" Lesson

>> So for the last section, I wanted to just clump as many of these things as we can into one big fun demo. And so we're gonna be creating a gossip network with hyper log for replication, we're gonna be using webrtc-swarm to create peer to peer connections in the browser.

And we're gonna maybe also using hyperkb and then we can make kind of like a shared Wiki by some very loose sense of that term, probably just like a text area that you can change and other people can see what the documents are. All right, so let's get started, here's some of the modules that we can use.

So oops, gonna create a new directory I'll call it wiki, okay, so the first thing that we need to do is we need to get our swarm setup, and we need to get our logs replicating on top of that swarm, then we can layer on more pieces once we have that working.

So, first thing that we need to do, Is create a new file main.js, this is gonna be running in the browser. So I'm gonna be using a lot of the same libraries we've already covered like local browserify and yo-yo to just, whip up a quick little interface. So the first thing that we wanna do is we'll require level, so that's gonna be level-browserify cuz we're gonna be in the browser.

We'll instantiate that call it wiki.db, I don't know call it whatever we want. And then we're gonna load hyperlog, so I think in this case, we're also gonna want to use sub level down to partition that database. So we're gonna require a sublevel down, we've got this database now that we can use for stuff.

So I'm gonna put a hyperlog into separate sublevel, so that we don't have to have a bunch of index dbs floating around. So we require hyperlog, and then we'll make a new log that will be hyperlog(sub(, so I like to call it subdb of call it 'log') and a set value in coding to Json as well, handy thing to do.

Okay, so now we've got some of the basics of our database set up, the next thing that we need to do is we need to set up a swarming logic over webrtc. So we can do that by requiring this module, webrtc-swarm, So webrtc-swarm does take a server that's going to do the introductions between peers because that's not something that webrtc can do by itself in a completely peer to peer fashion.

But this server is only used to set up the connections. It doesn't mediate that connections in any other way aside from just connecting people together. So we're gonna use a few modules through that Webrtc-swarm and signalhub. So we can create a new swarm, based on a signal hub name, so we'll call this one wiki, cool-wiki, we'll call it and then we need to pass in a list of signal hub addresses.

So there's one that happens to be running at signalhub.mafintosh.com who's the author who wrote this package. And I uses his server all the time for my little demos. So the swarm instance, will emit peer events when we have a peer connection, and that peer is a duplex stream.

So it means that whenever we get an incoming connection, we can write to it and we can also read from it. So, what we can do now is we can do log.replicate, and we can do live replication, which just means that once the two endpoints of the log are synced up the stream will stay open and then anytime either log gets a document written to it, it'll be synced up right away.

In true duplex stream fashion, a .pipe b.pipe a again, there we go, And then we're gonna need a way to add documents to our log. So I'll just bust out a quick little interface for doing that. So here we go, and then I'm gonna need to create do some dumb stuff.

So we need to append the document to the body, And we need to have a little update function. So our update function is just going to take little pieces of state, little variables in our program and it's going to update the DOM based on that information. So, one thing that we can do just to make sure that replication is working, is we'll have maybe a little text area where we can just log pieces of information.

This would be sort of like our wiki editing interface. And the pieces of information that we're gonna wanna keep around to start with would be maybe just a list of documents. So I'll just create a little array to do that, And now we need to set up our HTML so, We'll have a form.

It's gonna have a submit event, And then a text area with a name we'll call it content or yeah contents to find name for text area. And then in our submit handler, what we need to do is write out these documents to our database. And actually, I think we'll also want an input box that's gonna be like the title of our wiki page and we'll use this for the key later in our database.

Put some divs around that, Okay, so now in our on submit handler whoops. We're going to need to do this.elements.title.value to get the title name, in this.elements.content.value to get the message body. So content equals that and then like to get, we can reset, perform whatever okay, so then we need to write that data out to the log.

So we could do log.add if we knew the previous key. There's actually a shortcut in hyperlog called log.append, that just uses whatever the latest hash was that was written. So, We can use that in this case. Except what I'd actually like to do is wire up hyperkv to start with right now because it's gonna format the keys and values in a particular way.

So this will just make it easier for us to deal with. So, to set up hyperkv, we require it like so, we need to give it log in a database handle, so to instantiate it so, We call hyperkv with the hyper log, and then a database handle that's going to be a sublevel on our existing database.

So I'll call this one kv, right? Makes sense so far? Just using all of the pieces that we've seen. We haven't seen hyperkv in action until now, but this is how you set it up. And then you can call different methods like kv.put and kv.get, or in kv.createReadStream as well to list out all of the documents.

Okay, so now we can call kv.put with title and content, whoops. So the title is gonna be the key and the content is gonna be the value. I think we said JSON encoding, so why don't I do that, all right? By default, hyperkv is gonna do the same thing that log.append would do, where it's just kind of like a little shorthand, but for the latest known document.

But if we wanted to have explicit control over that, you can pass that in as a third option argument that is a links array. But we don't need to do that right now. I'll print out an error if there is one cuz that's good to know. Otherwise, we can print out the node object so we can see what it looks like in the hyper log, and then, Maybe we can take that documents array, turn it into an object, and then also set.

Set the document content, so, I think what we wanna do, though, is probably do, well, anyways, this is just gonna print out a message. I think this is enough code to write without testing it for now. So let's go ahead and run this thing. I'm just gonna use budo, like I've been doing for the other examples, run main.js.

Make sure that I call the update function right away, I do.
>> I think you're missing the TLD on your signal address, the .com.
>> Thank you, good call. I would just an awkward, I'm also gonna log out when I get a peer connection cuz that's, That's helpful to see.

Okay, so here we're now running. I'm gonna launch Chrome. I'm gonna do a normal Chrome, and then an incognito Chrome. Although the incognito will lose its database, which will be kinda useful because then the other one can sync to it when it pops back up again. Okay, so we go to localhost.

No, I thought I'd installed that one as well. Okay, so apparently I hadn't had that package as well. All right, now I have that library, took a while. Good, I just did not signalhub as well. All right, sorry, we gotta wait again, apologies. Now let's see if it works this time.

Okay, so it requests it. There we go, 4 megabytes of JavaScript, hooray! So that's fine. Excuse me, ignore those errors. So I'm gonna create a new document. I'm gonna call it hello. Gonna put a cool message there. I forgot to make a submit button, but I think if I hit Enter, I forgot to ev.preventDefault.

That's a common mistake, so I'm gonna go ahead and do that. Ev.preventDefault, cool. And, right, I have to be on the first box and hit Enter to make a submit, whatever. Okay, here we go. So we wrote a document now you can see it scrolled by. It's just like we were seeing on the command line with the kind of output.

So it has a key which is a hash. We could also have identity and signature if we had enabled the hyperlog sodium. And we see the links and we see the value, which is this message. So the messages in hyperlog are simple little key value internal to another key value format, yet another layer of keys and values.

k is the key, v is the value. So that's the body that we put into the textbox. So that all looks like it's working. So next thing that we need to do with this program is we need to go in and somehow list out all of the documents from our key value store.

So to do that, what we can do is I'll put that in a little function here at the bottom. Call it getlist, something like that. And our getlist function can modify that object that we have, the docs object. Okay, so there's createReadStream function is part of hyperkv. And I can pipe that into to.obj, like we've been doing a lot.

And just to start with, I'm gonna call getlist when the code loads, and we can just look at what values are in there. Hopefully we should see the document that we just created. Row= row next. Okay, so back here in the browser. Indeed, we see row=, and then we see the message that we already put.

So it's persisting in the browser into hyperlog. Without building a whole new interface, one really quick way that I can test to see if this is working is if I load this code into this incognito window here, which is not on the same IndexedDB. So it should be a clean IndexedDB.

Hopefully what I should see is if it can connect over WebRTC, But maybe it can't if it's seeing insecure response. Maybe I need to put HTTP instead of HTTPS for the signalhub. That may fix it.
>> It's like the Chrome course.
>> What?
>> It's one of the cross origin.

>> I'm just gonna check really quick what I did yesterday cuz that worked. [LAUGH] Okay, here's what I did yesterday, but it totally worked. All right, it was HTTPS. I think there's a bug where if you didn't put a slash in one version, then it wouldn't work. Maybe it is connecting and I should just leave it alone for a second, all right?

We'll see. All right, so another thing that I can try really quick is I think a budo has an option to do an SSL server. I might have to do that. You have to ignore the scary warnings So here we are, I have to put a new message because https is on a different origin policy from the default one.

I'll write that into the database, still not getting any peer connections though. Just too bad. Okay, yet another thing I can try, I installed Signal Hub, right? So it's going to be installed into here. Signal Hub happens to come with a bin package, which is like some library.

So if I listen, I can do a local host version of Signal Hub on a different port. So that might work. 5008, all right. I'm gonna Ctrl Shift s to reload this time. Now it's not happy about loading from an http page. So again, go back to the http version.

This stuff is super finicky sometimes but, When it works, it's really great. [LAUGH] Here we go, compiling four megabytes of JavaScript, takes a little bit of time. Okay, so, Hey, we have a peer connection. That's good. So what we need to do now is in that other database, so we have our get list function, which seems to work.

I'm just gonna, for a really simple hack, is I'm just going to put it in a set interval instead of wiring this all up properly, just so we can see if the replication worked. So, Look at that. So now, in the incognito window, we in fact see our documents from our key value store.

I could take the time to like build the proper interface, but I think by now it's good enough. In fact, if I add a document over here, I bet we can see it on the other side. So I'm gonna try that. Hi from incognito, hit Enter, and then gonna reload over here.

So it gotta establish a peer connection. Hey, there we go, hi from incognito. So we're syncing our key value store, which is built on a hyper log, which is a Merkle DAG, which has a materialized view sitting on top of it that gives us a really unusably obtuse wiki.

So there we go. [LAUGH]
>> Which could be refactored into something useful?
>> Yeah, which you, if you took the time, could make something quite useful. Use web technology, you could do that. Okay, great. [LAUGH] Any questions both on the Internet or in here? Sorry for the long pauses, but we finally made it to the end result that I was looking for.

So this was LevelDB in crypto. I think we did some of each of those things.
>> Awesome, thank you.
>> Great.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now