After working on the Understanding Streams in NodeJS article, I moved on to How to Stream Movies using NodeJS. Then I thought... Can I stream a Torrent in real time? Turns out that yes, I can! And it's surprisingly easy, thanks to the WebTorrent npm module.
And, to be honest, I'm completely blown away by this technology. I mean, the potential here is endless. I still can't believe that I didn't get it sooner. But better now than never. ;)
This repo is a tech demo/proof of concept/article showcasing what can you do using NodeJS and the WebTorrent module. This is also a good example of how powerful the idea of streams is in general. Because you'll see that even before the file is completely downloaded to the disk, we can read the chunks of data that we have. Proving that we don't need the whole thing to stream data to the client.
Follow the instructions on the Heroku main app page, and then once the app is deployed, visit the main page. Once you do that, you should see the site, with some examples of free Magnet Hashes so you can test out the live streaming.
And there you have it! Proof that you are streaming a Torrent live.
We know how we can stream binary files, thanks to the
.createReadStream() method, which gives us the possibility of specifying how much data should be read by setting the starting and finishing position of the chunk that we are interested in.
.createReadStream(), we don't care if the whole file is actually on the local drive, until we have the right section, we can read it and send it, while in the background the file is being constantly downloaded.
This also proves that we're actually dealing with only chunks, and are not loading the whole file into memory. If you want to learn more about streams in NodeJS, check out my previous article titled How to Stream Movies using NodeJS.
The code is composed of two parts: We have the front-end and we have the back-end. Shocking, I know. The good part is that the front has no UI design and only two lines of CSS. You can focus on the code itself. Aside from having very detailed comments, the code is also numbered to help you follow what is happening within each step.
The back-end is similar. If you read the How to Stream Movies using NodeJS article first, you see only new code related to the WebTorrent module. Meaning that if you feel confused, you should check How to Stream Movies using NodeJS first to understand the basics of streaming videos with NodeJS.
If you've enjoyed this article/project, please consider giving it a 🌟 or donate.
Also check out my GitHub account, where I have other articles and apps that you might find interesting.
If you'd like me to help you, I'm available for hire. Contact me at email@example.com.
You can follow me on social media 🐙😇, at the following locations:
I don’t only live on GitHub, I try to do many things not to get bored 🙃. To learn more about me, you can visit the following links: