open core »

Simple XMLHttpRequest with Fetch

This week, the new Fetch standard has landed in Chrome Canary (version 42). This is one of the specs I’ve been waiting for. It really simplifies XMLHttpRequests with a cleaner interface, and comes with built-in promises.

So to give you some perspective, here’s how you currently do an XMLHttpRequest:

var req = new XMLHttpRequest();

req.onload = function () {
};'GET', '', true);

This is a very simple example, and as you know, it can get much more verbose if you start handling error cases.

And now, this is the same request made with window.fetch() but with bonus error handling:

window.fetch('').then(function (response) {
    return reponse.text();
}).then(function (text) {
}).catch(function (e) {

In my opinion this is much simpler. And it’s also customizable:

window.fetch('', {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    body: JSON.stringify({ someField: 1234, anotherField: 'foobar' })
}).then(function (response) {
    return reponse.text();
}).then(function (text) {
}).catch(function (e) {

Have a look at the spec, there are much more features included.

What about other browsers support?

Github has released a Fetch polyfill that works perfectly well with all browsers (IE 9+ included), just make sure you also install es6-promise polyfill for promise support in older browsers.

Check out Github pull requests

This is a quick way to checkout Github pull requests into a local branch so you can test if everything is ok.

Just create this simple function in your ~/.bashrc or ~/.zshrc file:

function checkout-pr() {
    git fetch origin refs/pull/$1/head:PR$1

After saving, just reload your shell and the checkout-pr command should be available.

To use it just enter a project’s folder and type:

$ checkout-pr 13

This will access your project’s origin, fetch the content of the pull request with ID 13 and save it in a local branch called PR13.

Now you can checkout the PR13 branch and evaluate the changes made.

Yeoman generator for Ink

Probably everybody knows now that SAPO’s Ink framework is an awesome alternative to Bootstrap. And there’s also this great scaffolding tool called Yeoman, that if you don’t know, you should really check it out. The same also applies to Ink.

About two months ago, during my short travel to London to attend the The Great British Node Conf, I developed a Yeoman generator for Ink, called generator-ink.

This generator creates a ready-to-use website layout using Ink. You can even choose from 11 available layouts (more coming soon), and start hacking right away with the basic dependencies already in place.

To install it , just type on your terminal (make sure you already have Yeoman installed first):

$ npm install -g generator-ink

Now create a folder for your project and cd into it:

$ mkdir my_awesome_project && cd my_awesome_project

And now to start generator-ink just run:

$ yo ink

If everything went well, you’ll be greeted with the layout choice menu:

generator-ink layout choice menu

Now it’s just a matter of choosing the layout that most adjusts to your needs, and hack it.

Need more site pages? No problem, there’s a sub-generator for that. Imagine we need to create an article page. Piece of cake! Just type:

$ yo ink:page article-page article.html

You just need to know the name of the layout (you can check all available layouts here), give it a name, and BAM! It’s there!

You can also contribute with your own layouts! Just make a pull request on Github and I’ll be glad to accept it!

Playing RTMP streaming on Mac with VLC and rtmpdump

VLC is a great piece of software, but currently lacks RTMP support. The good news is that there might be some RTMP support planned for the 2.1 release.

Meanwhile, to fix this, here’s a simple workaround using rtmpdump.

First make sure you have rtmpdump installed. If not, you can install it using the Homebrew package manager:

brew install rtmpdump

Now make sure you have installed VLC and use the following command to play the RTMP streaming:

rtmpdump -r rtmp://foobar/live/stream --quiet | /Applications/ -

This will open VLC with a file named fd://0 on the playlist. Just click it and the streaming should be working.

You can create a vlc alias to make the command a lot simpler. Just add this in your .bashrc, .zshrc or .profile (choose with one applies) and restart the terminal:

alias vlc=/Applications/

If you don’t like to mess around with long terminal commands, here’s a bash function that makes it simpler:

function rtmp_open() {
        rtmpdump -r $1 --quiet | /Applications/ fd://0 --playlist-autostart

Just copy paste this into your .bashrc, .zshrc or .profile (choose which one applies), and restart the terminal.

I had to use fd://0 instead of - to refer to stdin due to a bug in VLC that ignores the --playlist-autostart when using - as input.

Now to run this handy function just type the name and the URL of the RTMP stream you want to play:

$ rtmp_open rtmp://foobar/live/stream

And that’s it. Enjoy :)

Raspberry Pi fun

Raspberry Pi logo

I got my Raspberry Pi three days ago, and I’ve been so busy “playing” with it, that I’m only writing this post now.

If you’re living under a rock, let me tell you that the Raspberry Pi is an awesome credit-card sized computer that costs $35. But the Raspberry Pi website puts it better:

The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. It’s a capable little PC which can be used for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. We want to see it being used by kids all over the world to learn programming.

Yep, that’s right: kids. And guess what? All the grown ups are loving it :)

It’s mind-blowing the tons of cool things you can do with this little computer. The first thing I did with it was to install Raspbian Wheezy image on the SD card (Debian for the RPi, available in the Downloads section of the RPi website) and put a Node server running on it. The performance is pretty acceptable.

And there’s also the community around the RPi. All kinds of people from experienced programmers to beginners are showing off everyday new things that can be done. It’s a nice way to learn about the lower level stuff that goes on in your computer, and you can use it to control other stuff too, like robots, leds etc.

I’ll be writing here some of my experiences with the RPi as I get to know it better.

Meanwhile, here’s a photo of the unboxing:

Raspberry Pi unboxing

And a photo of the RPi running headless and being configured through SSH:

Raspberry Pi up and running

I bought it from InMotion, a portuguese online store. So if you are in Portugal, I highly recommend buying your Rpi from this store. It costs about 42€ and the service is very fast (ordered on a thursday and had it on monday).

So go get one, and feed your inner geek :)

Nodo v0.2.0 released

A new version of Nodo is out. Version v0.2.0 has the following features:

  • New actions star and unstar that makes possible to mark ou unmark a task as important.
  • UI improvements and some bug fixes.

This new version is now available on NPM. To upgrade use:

npm udpate -g nodo

Please report any issues you find on the github issues page.

Nodo, the simple command line task manager

Nodo is a command line task manager, and I love it! Why? Mainly because I developed it, second, it does exactly what I needed from a TODO app: it’s simple, easy and of course, it’s on the command line.

I spend most of the time on the command line (I’m even writing this post on it with Vim) so it comes natural the fact that I needed to find a task management application that has a CLI, avoiding the constant switch from keyboard to mouse and vice-versa. But still, I used Wunderlist, an awesome mac/windows/iphone/android TODO app, that was lacking such interface.

But there are already command line task managers!

Yes it’s true. I’m not going to mention them all, but let’s just say that none of them met my criteria. Some where too simple, some were just too complex (like TODO.txt) with tons of commands and options. Also, none of them supported my current Wunderlist database.

So I developed one task manager that suited my needs.

In Node.js? But I thought that Node.js was only good at X

Well, it turns out that Node is actually pretty good at allot of stuff. Including command line apps. But I have to agree that it’s not the best choice because there are no advantages in having non-blocking IO in this kind of app. But since I’ve been learning allot of Node lately, I really wanted to put in practice all my newly aquired skills. Also, NPM is a great package manager, and it’s ridiculously easy to publish new packages with it, not mentioning that it’s very interesting the fact that it’s entirely written in CouchDB.

Ok, so how does Nodo work?

It’s pretty simple. It has an NPM package, so you can install it with the npm command, just like this:

$ npm install -g nodo

A few seconds later, Nodo will be installed on your machine.

After install, Nodo comes with a default list called “inbox”. This list is where you should put all the tasks that you are not sure in which list to put them. So, to add a task to the inbox list just type the following:

$ nodo add inbox go shopping

Later, you decide to organize your tasks, so let’s create a list called “home” to contain all the home related tasks:

$ nodo add list home

And now, you can move your shopping task to this new list. Assuming that the shopping task has id 3:

$ nodo move 3 home

After you go shopping you can mark this task as done:

$ nodo done 3

This is just the Nodo basics. For the full usage instructions I recommend watching the screencast below, or visiting the Nodo page on Github.

Currently Nodo does not support adding/editing task notes, or setting a task as important, but these are all just features that are coming in the near future. Feel free to suggest features and/or fixes.