Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use webtorrent in Node? #183

Closed
daver182 opened this issue Nov 13, 2014 · 1 comment
Closed

How to use webtorrent in Node? #183

daver182 opened this issue Nov 13, 2014 · 1 comment

Comments

@daver182
Copy link

@daver182 daver182 commented Nov 13, 2014

Hi, I'm trying to use webtorrent in Node but I'm getting too many errors, beside that maybe the docs arent too explanatory.

I have this:

require('fs');

var WebTorrent = require('webtorrent');
var client = new WebTorrent();

fs.readFile('data/data1.json', 'utf8', function (err,data) {
    if (err) return console.log(err);

    var buffer = new Buffer(data, "utf-8")
    client.seed(buffer, function(torrent){
        console.log(torrent);
    });
});

I've tried this other way:

fs.createReadStream('data/data1.json', {}).on('data',function(data){
    console.log(data.length);
    client.seed(data, function(torrent){
        console.log(torrent);
    });
});

The errors I get are in the new Torrent call, specifically in torrent.lib line 88:

TypeError: path must be a string

What I'm doing wrong? Is this the good aproach? The file is a JSON type, maybe thats the problem.

Thanks for any help!

@feross

This comment has been minimized.

Copy link
Member

@feross feross commented Nov 27, 2014

Hi @daver182, thanks for trying out WebTorrent. What errors are you encountering in node.js? Without seeing the exact errors I can only guess at the problem.

Your first example looks almost correct, except for one issue. In node you need to assign the result of require to a variable:

var fs = require('fs');

Also, it could be simplified like this:

var fs = require('fs');
var WebTorrent = require('webtorrent');

var client = new WebTorrent();

client.seed('data/data1.json', function (torrent) {
  console.log(torrent);
});

You can see what values client.seed takes as input by reading the documentation for client.seed.

Your second example has many more issues. That's not how streams work. I would read the streams handbook and complete the stream-adventure nodeschool workshop to learn how to use streams correctly :)

Feel free to reply if you have additional issues with WebTorrent.

@feross feross closed this Nov 27, 2014
@lock lock bot locked as resolved and limited conversation to collaborators May 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.