Coder Social home page Coder Social logo

davidgatti / how-to-stream-torrents-using-nodejs Goto Github PK

View Code? Open in Web Editor NEW
78.0 5.0 25.0 45 KB

๐Ÿฟ How to Stream Torrents using NodeJS and the HTML5 Video tag

Home Page: https://david.gatti.io

License: MIT License

JavaScript 94.95% Shell 0.73% Dockerfile 4.32%
stream-movies stream-torrents nodejs article webtorrent

how-to-stream-torrents-using-nodejs's Introduction

๐Ÿฟ How to stream Torrent movies using NodeJS and the HTML5 video tag

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. ;)

What is this repository

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.

Deployment

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.

Where to start

  1. Visit the home page of your deployment
  2. Click on one of the demo Hashes
  3. You should see the Client Stats start to show some stats
  4. In a moment, you should see the content of the Magnet Hash
  5. Select a movie file from the Magnet Contents section
  6. After few seconds, you should see the video player buffering the movie
  7. It should start playing shortly

And there you have it! Proof that you are streaming a Torrent live.

High level explanation

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.

With .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.

How to understand the code

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.

The End

If you enjoyed this project, please consider giving it a ๐ŸŒŸ. And check out my GitHub account, where you'll find additional resources you might find useful or interesting.

Sponsor ๐ŸŽŠ

This project is brought to you by 0x4447 LLC, a software company specializing in building custom solutions on top of AWS. Follow this link to learn more: https://0x4447.com. Alternatively, send an email to [email protected].

how-to-stream-torrents-using-nodejs's People

Contributors

davidgatti avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

how-to-stream-torrents-using-nodejs's Issues

Can't detect invalid magnet hash

is there any way to detect invalid hash, because I've tried an invalid hash and received no error at all, in fact, nothing happened!
is there any fix for it?
thanks.

not work on Heroku

I have tried to do the installation in heroku, but it shows an error in

[email protected] install /tmp/build_beb57d8d/node_modules/bufferutil
remote: > node-gyp-build
remote:
remote: make: Entering directory '/tmp/build_beb57d8d/node_modules/bufferutil/build'
remote: CC(target) Release/obj.target/bufferutil/src/bufferutil.o
remote: ../src/bufferutil.c:3:10: fatal error: node_api.h: No such file or directory
remote: 3 | #include <node_api.h>

I don't know how I can fix this, could you help me?

Need help urgent

hi i just deployed ur app on heroku it works flawlessly but please tell me how to download the video whenever i click on the download button it do not download please help

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.