first look

Hi friends,

I saw a project on freelancer and that wanted to see phonegap and in action. I put on my thinking cap and started exploring. is a beautifully brewed concept where sockets are used for transport.


There are namespaces, which you can create and the client will connect to that.

// the following two will emit to all the sockets connected to `/`
io.sockets.emit('hi', 'everyone');
io.emit('hi', 'everyone'); // short form

Each namespace emits a connection event that receives each Socket instance as a parameter.

io.on('connection', function(socket){
  socket.on('disconnect', function(){ });


Also, sockets can be put in channels. When an socket is initialized, it is part of a room which is equal to its socket id.

io.on('connection', function(socket){
  socket.join('some room');

And then simply use to or in (they are the same) when broadcasting or emitting:'some room').emit('some event'):

To leave a channel you call leave in the same fashion as join.

Default room

Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket automatically joins a room identified by this id.

This makes it easy to broadcast messages to other sockets:

io.on('connection', function(socket){
  socket.on('say to someone', function(id, msg){'my message', msg);


Upon disconnection, sockets leave all the channels they were part of automatically, and no specially teardown is needed on your part.

Knowing this, I leaped into code.

The client side code is as below:

<!doctype html>
    <title>Pingu chat</title>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    <script src=""></script>
    <script src=""></script>
      var socket = io.connect('');
        socket.emit('chat message', $('#m').val());
        return false;
      socket.on('chat message', function(msg){

And the server side code is:

var app = require('express')();
var http = require('http').Server(app);
var io = require('')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);

http.listen(8080, function(){
  console.log('listening on *:8080');

To start serving through node, run the commend (preferably in screen, so that it keeps running):

node index.js

So, the app Pingu chat is running on: