Real Time Meteor.js http.get


Question

I am looking to develop a new application that relies on talking to an external service API.

For example, I want to create a real time Twitter feed that updates everytime there is a new tweet, I want to use Meteor as the framework but I'm not sure if it's possible to have Meteor display new tweets automatically without a page refresh.

I know I could do this with Node.js and Socket.io but is it possible to do in solely Meteor?

Thanks

1
3
11/2/2012 11:00:21 AM

There are basically two methods for retrieving data from an external source. Ajax or http request on the server. I recently tackled this, but had to use the second method.

Client.js

Meteor.startup( function() {
    Meteor.call( 'openSession', function( err, res ) {
        if( !err ) Session.set( 'data', res );
    });
});

Server.js

Meteor.methods({
    openSession: function() {
        var fut = new Future(), url = 'http://www.google.com';

        // Do call here, return value with Future
        Meteor.http.get(url, function( err, res ){
            fut.ret(res);
        });

        // Force method to wait on Future return
        return fut.wait();
    }

});

As you can see, I had to use Future to make Meteor play along with the asynchronous http request. However, it's as simple as defining the method in your server side, then calling that on your client side.

5
11/3/2012 1:16:49 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon