Transcript from the "Level-Livefeed" Lesson
>> Another fun package. This one, unfortunately is a little bit out of date. I think we're gonna send a pull request pretty soon to update it with a couple things, but it still works. It's called level-livefeed. So if you've used a database like Couch TV, or there's other ones, that have live feed that you can subscribe to, to get the most recent changes.
[00:00:21] This is really useful if you wanna build something that uses WebSockets and can sort of like push the updates that you're getting directly from the database into users' web browsers without having to have a second cue system or whatever. So this is a kinda nice thing. You can mpm install it by doing mpm install level-livefeed.
[00:00:45] And then you pass in the database and you can give it some options, like where the key should start and end. So you can do some basic filtering on it. So why don't we go ahead and make one of those as well, just to show you how it works.
[00:00:59] And we'll do something fun after that. So set up our database like before, I'm just gonna copy from books from sub.js, just to get that part, alright, so we'll call it live.db. We're gonna set up a live feed by requiring level-livefeed. So you might have noticed, we've done this twice already.
[00:01:30] These libraries like subleveldown and level-livefeed, don't require level themselves. Instead you're always passing in the database instance to them. There's a number of reasons for this, one is that if you can manage the database instance yourself, you have control over the versioning for what version of leveldb you're using.
[00:01:57] So you don't have this peer dependency relationship with these kind of implicit dependencies. And also, there are more leveldbs than just the one called level on npm. There's actually lots of different kinds of back ends. So when you pass in the database instance, rather than having the module do it for you, then you have a lot of benefits.
[00:02:17] And you have a lot of flexibility to use. Browser-based leveldbs memory, in memory, leveldb interfaces. Or maybe even SQL-based leveldbs. I think there's even a MongoDB wrapper, or a leveldb wrapper on top of MongoDB and all of these other databases. I've never personally needed those kind of things, but they're there.
[00:02:40] And something that you get for free if you stick to these basic conventions about the interfaces. So anyways, level-livestream can take on options hash of stuff, but you get back a readable stream. So, yet again, we can just consume that stream and print it out. It's an object stream, just like createReadStream is with db.createReadStream.
[00:03:04] So, it's like I don't have to save that, I can just pipe it directly like this. Make sure to set object mode, when you're consuming these streams. Then, you get every objects, the encoding, which you can ignore, and we'll just print out what's gonna be written. So to have this be a real example, we need to actually write some data to this database.
[00:03:28] So I'm gonna just create a little setInterval to do that real quick. So setInterval, every 500 milliseconds, we will just db.put some random garbage, so like hello and then Date.now(). And if there's an error we'd probably want to know about it. Okay, so now, even though we're not printing out everything that we're writing, these inserts and modifications to the database can come from anywhere in our code base.
[00:04:09] And we can sort of get a handle on what's coming down the pipe in one place, which is kinda nice. So just run that. Hang on a sec. What, livestream, sorry, I just made a typo, that's all. [LAUGH] Okay, so now we've got this example and I can run it.
[00:04:41] And then we should see our data come down the pipe, there we go. So still wait to get data that comes down the pipe. I don't know why the value is a function but, right because I actually didn't put anything there. Let's put Date.now again. Okay, so now we've got a proper example wherever we're putting real data in.
[00:05:05] Yeah, that was all of the existing data because by default level-livestream will pick up from the beginning of the inserts into the database. So if we want it not to do that, we can, I think, set an option called tail, To be true. And then it will just show us the most recent changes.
[00:05:26] Or whatever, there's like some parameter in the documentation to do that. Okay, so that's some examples of some modules that you're likely to see and use on NPM. And, again, you should, if you can, should try to pass in the leveldb instance rather than requiring it somewhere in the bowels of your package.
[00:05:49] And this is kind of related to the idea of the IO shell in programming architecture where you try to push up the decisions about customizations to kind of higher levels when you can.