A dead crappy simple node.js chat with telnet

Stolen from http://www.youtube.com/watch?v=jo_B4LTHi3I Prerequisite: node.js installed (http://howtonode.org/how-to-install-nodejs)

Usage:

create chat.js (with the code from down under)

  • type node chat.js in the terminal
  • open a new terminal-window
  • telnet 127.0.0.1 8000
  • chat away

Bonus: on a local company net? get your IP (ifconfig) and telnet into that, share it with your friends.

var net = require('net')

var sockets = [];

var server = net.createServer(function(socket) {
 
  sockets.push(socket); 
    
  socket.on('data', function(d) {
    for (var i = 0; i < sockets.length; i++) {
      if (sockets[i] == socket) continue;
      sockets[i].write(d);   
    }
   
  });
  
  socket.on('end', function(d) {
    var i = sockets.indexOf(socket);
    sockets.splice(i,1);
  });
  
});