It’s been couple of weeks I have started using CoffeeScript (I know it has awesome integration with Ruby on Rails and particularly Node based projects) and I have fallen in love with it.
It’s not like that I don’t like JavaScript but JavaScript is a hard language. I think JavaScript is one of the language that most programmers have used in some ratio and most will say claim they know that language and most of the time it may be kind of wrong assumption.
For those who haven’t used CoffeScript, its a Ruby inspired language (or micro framework) that compiles into JavaScript.
I’ve been using CoffeeScript for couple of projects now. JavaScript source (http://cdn.sailthru.com/horizon/v1.js ) for Horizon, Sailthru‘s behavioral tracking product is generated using CoffeeScript and another one is work in progress, Node.js client for Sailthru API.
Not to forget that CoffeeScript makes using Node more fun!
Here’s dead simple Node server using CoffeeScript:
server = require('http').createServer (request, response) ->
response.writeHead 200, {'Content=Type': 'text/plain'}
response.end 'Hello World'
server.listen 9999
console.log 'Sever running at http://0.0.0.0:9999'
And generated source code:
(function() {
var server;
server = require('http').createServer(function(request, response) {
response.writeHead(200, {
'Content=Type': 'text/plain'
});
return response.end('Hello World');
});
server.listen(9999);
console.log('Sever running at http://0.0.0.0:9999');
}).call(this);
I like the fact that CoffeeScript makes you hard to have global variable because everything is wrapped inside an anonymous function. And of course you can always use global variable in the form of attaching to window object in JavaScript and attaching to exports object for Node systems.
Node.js might be the most exciting single piece of software in the current JavaScript universe. Ryan received standing ovations for his talk and he really deserved it!
I tried Node.js last week and I was quite impress. Truly, Node.js has the potential to change the way we do I/O. Basically it’s an evented programming concept build on underlying stack of Python and C++ while using Google’s V8 (JavaScript Engine) as an interfacing language capable of performing network programming (using polling) in different way. Here are some of the key design goals of this technology:
No function should direct perform I/O (Use Callback to do so)
Stream everything, never force the buffering of data (Somewhat like comet)
Have built-in support for the most important protocols: HTTP, DNS, TCP
Support many HTTP features: Chunked requests, Keep-alive, Hang Requests for comet applications
Bridge API somewhere between flexibility of client side JavaScript and old Unix tools
Platform independent
Example:
Simple Twitter Client I wrote last week:
var http = require("http"),
sys = require("sys");
var connection = http.createClient(80, "twitter.com");
var since_id = '';
var interval = 0;
function getTweets() {
var url = "/statuses/friends_timeline.json";
if (since_id != '') {
url += "?since_id=" + since_id;
}
var request = connection.get(url, {
"content-type": "application/json",
"User-Agent": "NodeJS HTTP Client by Infynyxx",
"host": "twitter.com",
"Authorization": "Basic " + Base64.encode("infynyxx:*****")
});
request.finish(function(response) {
var responseBody = "";
response.setBodyEncoding("utf8");
response.addListener("body", function(chunk) {
responseBody += chunk;
});
response.addListener("complete", function() {
var tweets = JSON.parse(responseBody);
if (tweets.error) {
sys.puts("Error: " + tweets.error);
}
else {
var length = tweets.length;
if (length > 0) {
sys.puts("Getting new tweets...\n");
sys.puts("Number of new tweets: " + length + "\n");
var str = "";
tweets.reverse();
tweets.forEach(function(element, index) {
str += element.text + "\n";
str += element.user.name + "\n";;
str += element.created_at + "\n";;
str += "*************************\n";
});
sys.puts(str);
}
}
});
});
setTimeout(getTweets, interval);
interval = 300000; //5 minutes
}
getTweets();