public
Last active

Meteor: Make HTTP calls to remote services (http://www.jumpbyte.com/2012/meteor-make-ht…remote-service/)

  • Download Gist
app.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
if (Meteor.is_client) {
var userName = "PatelNachiket";
Template.hello.greeting = function () {
return "Fetch recent tweets from Twitter stream of user : " ;
};
 
Template.hello.events = {
'click #fetchButton' : function () {
console.log("Recent tweets from stream!");
$('#fetchButton').attr('disabled','true').val('loading...');
userName = $('#userName').val();
Meteor.call('fetchFromService', userName, function(err, respJson) {
if(err) {
window.alert("Error: " + err.reason);
console.log("error occured on receiving data on server. ", err );
} else {
console.log("respJson: ", respJson);
//window.alert(respJson.length + ' tweets received.');
Session.set("recentTweets",respJson);
}
$('#fetchButton').removeAttr('disabled').val('Fetch');
});
}
};
Template.hello.recentTweets = function() {
return Session.get("recentTweets") || [];
}
Template.hello.userName = function() {
return userName;
}
}
 
if (Meteor.is_server) {
Meteor.methods({
fetchFromService: function(userName) {
var url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name="+userName+"&count=10";
//synchronous GET
var result = Meteor.http.get(url, {timeout:30000});
if(result.statusCode==200) {
var respJson = JSON.parse(result.content);
console.log("response received.");
return respJson;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throw new Meteor.Error(result.statusCode, errorJson.error);
}
}
});
}
home_tpl.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<head>
<title>Meteor - twitter public timeline demo</title>
</head>
 
<body>
{{> hello}}
</body>
 
<template name="hello">
<h3>Meteor: calling 3rd party service</h3>
{{greeting}} <input type="text" id="userName" value="{{userName}}"></input>
<input type="button" value="Fetch" id="fetchButton" />
 
<ul>
{{#each recentTweets}}
<li>{{text}}</li>
{{/each}}
</ul>
</template>

Blog about this gist: http://www.jumpbyte.com/2012/meteor-make-ht…remote-service/
live demo: http://remoteservice.meteor.com
Steps to run this sample code
1. Install Meteor
2. Create a new meteor project by meteor create
3. replace app.js file
4. add home_tpl.html and remove auto created .html file

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.