Friday, February 15, 2013

Chat server in node.js

Continuing to watch Ryan's excellent introduction of node.

He started to write a chat server.  I looked up a bit in the API docs and added a little.


net = require('net');
var sockets = [];
var server = net.Server(function(socket) {
  sockets.push(socket);
  socket.write("Welcome to the chat room.\n");
  socket.on('data', function(data) {
    for (var i = 0; i < sockets.length; i++) {
      sockets[i].write(socket.remoteAddress + ":" + socket.remotePort + "> " + data);
    }
  });
  socket.on('end', function() {
    var i = sockets.indexOf(socket);
    sockets.splice(i, 1);
  });
});

server.listen(8000);

Thursday, February 14, 2013

Getting Started with node.js and IntelliJ IDEA 12.0

Today I began with node.js.  My overall goal is to develop some JavaScript chops.

IntelliJ IDEA

I also finally unwrapped my IntelliJ IDEA 12.0 install.  I bought this bad boy back when JetBrains was having a fire sale to celebrate the end of the world. :)


  • the NodeJs plugin doesn't come bundled with IntelliJ, I had to download it.
  • I created a "Node Boilerplate" project.  Not sure what that is, yet.
  • there's a script ya run that initializes the project.
    • it used npm to download a bunch of dependencies: Jade, Mocha, socket.io, should, growl, formidable, mongodb, and a bunch of others.
    • it then seemed to run a mongodb installer.  That included this comment:
      "To install with C++ bson parser do <npm install mongodb --mongodb:native>"
    • it then showed a dependency tree:
should@1.2.1 ./node_modules/should 
vows@0.5.13 ./node_modules/vows 
└── eyes@0.1.8
jade@0.20.0 ./node_modules/jade 
├── commander@0.2.1
└── mkdirp@0.3.4
express@2.5.2 ./node_modules/express 
├── mkdirp@0.0.7
├── mime@1.2.9
└── qs@0.5.3
connect@1.8.5 ./node_modules/connect 
├── mime@1.2.9
├── formidable@1.0.11
└── qs@0.5.3
mocha@1.8.1 ./node_modules/mocha 
├── growl@1.7.0
├── commander@0.6.1
├── debug@0.7.2
├── diff@1.0.2
├── mkdirp@0.3.3
├── ms@0.3.0
└── jade@0.26.3
mongoose@2.4.8 ./node_modules/mongoose 
├── colors@0.5.1
├── hooks@0.1.9
└── mongodb@0.9.7-2-5
socket.io@0.8.7 ./node_modules/socket.io 
├── policyfile@0.0.4
├── redis@0.6.7
└── socket.io-client@0.8.7

I noticed that the same top-level packages were listed in $project/node_modules/


Ummm... wow, that's kindof a lot to start with.  All I really want, right now, is just a simple node.js app.

Simple node.js

Heading over to the node.js site itself, I found this intro tutorial: http://www.youtube.com/watch?v=jo_B4LTHi3I

This was watching Ryan Dahl playing with node.js

  • process is a global object not unlike the "window" object in JavaScript-in-the-browser environments
  • you cannot sleep in node.  You never halt execution.
  • node exists when there's nothing left to do.  This includes stuff like registered callbacks.

Day 0

The morning I started this blog, sitting on the Metrolink 682 waiting for it to leave Los Angeles Union Station, I had finally decided to take matters into my own hands.

For too long I've been a slave to my inbox.  I've burned countless hours, days, weeks on processing email.  It's ridiculous!  In many other ways I've allowed numerous other shiny objects to distract me from my real true love: programming.

I don't have much of an agenda.  Really, I just want to regain and expand my technical chops.  This is my deliberate practice time.