Introduction to Node.js and what I find so great about it

My knowledge of the inner workings of node.js and threading is pretty basic. I can’t guarantee everything written here is accurate. If you spot an error, please let me know!

I’ve been playing around with Node.js for a while now and even though the community behind it is getting bigger, there are a lot of people disliking a lot of things about Node.js. In this post I will explain some of the different inner workings and what I like about them.

I hear you ask yourself: “Why Javascript?”. Even though a lot of people think Javascript is slow, it’s not so bad actually. Note that a lot of things that make it slow in the browser (like the really slow DOM api) are non existent on the server.

From the Node.js homepage:

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Node.js allows you to write your server side scripts in javascript. The V8 engine of Google (which is also a part of Chrome, and is one of the big reasons on why Chrome is so fast) will take your javascript scripts and compile them on the fly to machine code.

But the thing that is most different from Node to traditional server sided languages for doing web stuff is the way it works.

more

How to connect your Arduino to nodejs using duino

In this video I’ll explain how to set up your Arduino and Duino so you can interact with your Arduino using nodejs (and javascript).

  • The Arduino software package is downloadable from the Arduino website.
  • You can find the Duino module here.
  • Installing Duino is as simple as running npm install duino.
  • My custom bleep function:

    var arduino = require(‘duino’),

    board = new arduino.Board();
    

    var led = new arduino.Led({ board: board, pin: 13 });

    var bleep = function() {

    led.on();
    setTimeout(function() {
        led.off();
    }, 100);
    

    }

Troubleshooting:

  • You need a recent version of nodejs (0.4.x was not working) for the Duino module.
  • If you can’t upload the Arduino program, make sure the right model is selected at Tools -> Board.

Using Github to deploy your code

My VPS is my ‘live server’, it runs all my production code on the internet. But I develop locally on my laptop using XAMPP. For most of my projects I use Git as a version control system while storing it on Github.

So the infrastructure looks like:

 laptop   ->   Github     ->     vps
   dev      version control    production
working copy     repo         working copy

Everytime I make some changes to my code I commit them to Github:

git commit -a
git push

When it’s time to deploy an update I only have to get the latest changes from Github:

git pull

more

Posted at September 01, 2012, under Git.

How to easily run a PHP script in the background via terminal

I’m writing a PHP script that is going to serve as a daemon on my own server. As I’m writing it I want to test it a lot to make the daemon as stable as possible. Yesterday I wanted to let the script run overnight to monitor memory usage and make sure it didn’t crash.

If you don’t want to bother setting up a cron you can just run:

nohup php [file.php] &

All the output (stuff you echo) will be stuffed in nohup.out.

Posted at August 02, 2012, under PHP.

Visualizing our running progress

You’ve found a dinosaur, this post is only here for archiving purposes. The content is outdated and is not applicable anymore.

While I was running a couple of weeks back I was thinking about Nike’s new slogan for it’s Nike+ campaign. “Make it count!” it says, but for what? So I hacked up a little website that fetches all tweets send by the Nike+ app and bundles all our runs together per hour.

Make it count! is the result. It holds a realtime visualisation which displays all runs reported by Twitter as circles. Big circles are big runs, etc.. You can also see the top hour of today (since midnight GMT). Every hour get’s a score and our goal is to top that score every hour!

The site is built almost entirely in javascript, node.js catches all tweets and sends them over realtime by using web sockets (socket.io). You can read more about that in my previous blogpost.

The site does need some improvement in the design, I’ve already talked to some people about helping me out here. So stay tuned for even more awesomeness!