diff --git a/README.md b/README.md index 1427fc64..4d24927b 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,8 @@ client.add(magnet_uri, function (torrent) { }) ``` +There are more examples in the [examples](https://github.com/feross/webtorrent/tree/master/examples) folder. + ##### Browserify WebTorrent works great with [browserify](http://browserify.org/), an npm module that let's diff --git a/examples/browser-download.js b/examples/browser-download.js new file mode 100644 index 00000000..3428f72d --- /dev/null +++ b/examples/browser-download.js @@ -0,0 +1,22 @@ +var WebTorrent = require('webtorrent') + +var client = new WebTorrent() + +client.add(magnet_uri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + torrent.files.forEach(function (file) { + // Get a url for each file + file.getBlobURL(function (err, url) { + if (err) throw err + + // Add a link to the page + var a = document.createElement('a') + a.download = file.name + a.href = url + a.textContent = 'Download ' + file.name + document.body.appendChild(a) + }) + }) +}) diff --git a/examples/browser-seed.js b/examples/browser-seed.js new file mode 100644 index 00000000..6a6d498e --- /dev/null +++ b/examples/browser-seed.js @@ -0,0 +1,12 @@ +var dragDrop = require('drag-drop/buffer') +var WebTorrent = require('webtorrent') + +var client = new WebTorrent() + +// When user drops files on the browser, create a new torrent and start seeding it! +dragDrop('body', function (files) { + client.seed(files, function onTorrent (torrent) { + // Client is seeding the file! + console.log('Torrent info hash:', torrent.infoHash) + }) +}) diff --git a/examples/browser-stream-to-audio.js b/examples/browser-stream-to-audio.js new file mode 100644 index 00000000..84dfdfd3 --- /dev/null +++ b/examples/browser-stream-to-audio.js @@ -0,0 +1,19 @@ +var WebTorrent = require('webtorrent') + +var client = new WebTorrent() + +client.add(magnet_uri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + // Let's say the first file is an mp3 audio file + var file = torrent.files[0] + + // Create an audio element + var audio = document.createElement('audio') + audio.controls = true + document.body.appendChild(audio) + + // Stream the audio into the audio tag + file.createReadStream().pipe(audio) +}) diff --git a/examples/browser-stream-to-video.js b/examples/browser-stream-to-video.js new file mode 100644 index 00000000..7943a643 --- /dev/null +++ b/examples/browser-stream-to-video.js @@ -0,0 +1,19 @@ +var WebTorrent = require('webtorrent') + +var client = new WebTorrent() + +client.add(magnet_uri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + // Let's say the first file is a webm (vp8) or mp4 (h264) video... + var file = torrent.files[0] + + // Create a video element + var video = document.createElement('video') + video.controls = true + document.body.appendChild(video) + + // Stream the video into the video tag + file.createReadStream().pipe(video) +}) diff --git a/examples/node-save-to-file-system.js b/examples/node-save-to-file-system.js new file mode 100644 index 00000000..5b33d0cb --- /dev/null +++ b/examples/node-save-to-file-system.js @@ -0,0 +1,16 @@ +var WebTorrent = require('webtorrent') +var fs = require('fs') + +var client = new WebTorrent() + +client.download(magnet_uri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + torrent.files.forEach(function (file) { + // Stream each file to the disk + var source = file.createReadStream() + var destination = fs.createWriteStream(file.name) + source.pipe(destination) + }) +})