SilentDragon
SilentDragon

Reputation: 73

Can I use other node.js libraries in Meteor?

I was playing around with an idea and wanted to get some json from another site. I found with node.js people seem to use http.get to accomplish this however I discovered it wasn't that easy in Meteor. Is there another way to do this or a way to access http so I can call get? I wanted an interval that could collect data from an external source to augment the data the clients would interact with.

Upvotes: 6

Views: 1576

Answers (3)

Rajanand02
Rajanand02

Reputation: 1303

You can use Meteor.http if you want to handle http. To add other node.js libraries you can use meteorhacks:npm

meteor add meteorhacks:npm

Create apacakges.json file and add all the required packages name and versions.

{
  "redis": "0.8.2",
  "github": "0.1.8"
}

Upvotes: 0

debergalis
debergalis

Reputation: 11870

This is much easier now with Meteor.http. First run meteor add http, then you can do something like this:

// common code
stats = new Meteor.Collection('stats');

// server code: poll service every 10 seconds, insert JSON result in DB.
Meteor.setInterval(function () {
  var res = Meteor.http.get(SOME_URL);
  if (res.statusCode === 200)
    stats.insert(res.data);
}, 10000);

Upvotes: 8

Adam Blinkinsop
Adam Blinkinsop

Reputation: 152

Looks like you can get at require this way:

var http = __meteor_bootstrap__.require('http');

Note that this'll probably only work on the server, so make sure it's protected with a check for Meteor.is_server.

Upvotes: 8

Related Questions