Click here to Skip to main content
15,896,269 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 361.8K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
/*!
 * socket.io-node
 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
 * MIT Licensed
 */

(function (module, io, should) {

  if ('object' == typeof global) {
    return module.exports = { '': function () {} };
  }

  module.exports = {

    'test connecting the socket and disconnecting': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('connect', function () {
        socket.disconnect();
        next();
      });
    },

    'test receiving messages': function (next) {
      var socket = create()
        , connected = false
        , messages = 0;

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('connect', function () {
        connected = true;
      });

      socket.on('message', function (i) {
        String(++messages).should().equal(i);
      });

      socket.on('disconnect', function (reason) {
        connected.should().be_true;
        messages.should().equal(3);
        reason.should().eql('booted');
        next();
      });
    },

    'test sending messages': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('connect', function () {
        socket.send('echo');

        socket.on('message', function (msg) {
          msg.should().equal('echo');
          socket.disconnect();
          next();
        });
      });
    },

    'test manual buffer flushing': function (next) {
      var socket = create();

      socket.socket.options['manualFlush'] = true;

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('connect', function () {
        socket.socket.connected = false;
        socket.send('buffered');
        socket.socket.onConnect();
        socket.socket.flushBuffer();

        socket.on('message', function (msg) {
          msg.should().equal('buffered');
          socket.disconnect();
          next();
        });
      });
    },

    'test automatic buffer flushing': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('connect', function () {
        socket.socket.connected = false;
        socket.send('buffered');
        socket.socket.onConnect();

        socket.on('message', function (msg) {
          msg.should().equal('buffered');
          socket.disconnect();
          next();
        });
      });
    },

    'test acks sent from client': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('connect', function () {
        socket.on('message', function (msg) {
          if ('tobi 2' == msg) {
            socket.disconnect();
            next();
          }
        });
      });
    },

    'test acks sent from server': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('connect', function () {
        socket.send('ooo', function () {
          socket.disconnect();
          next();
        });
      });
    },

    'test connecting to namespaces': function (next) {
      var io = create()
        , socket = io.socket
        , namespaces = 2
        , connect = 0;

      function finish () {
        socket.of('').disconnect();
        connect.should().equal(3);
        next();
      }

      socket.on('connect', function(){
        connect++;
      });

      socket.of('/woot').on('connect', function () {
        connect++;
      }).on('message', function (msg) {
        msg.should().equal('connected to woot');
        --namespaces || finish();
      }).on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.of('/chat').on('connect', function () {
        connect++;
      }).on('message', function (msg) {
        msg.should().equal('connected to chat');
        --namespaces || finish();
      }).on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });
    },

    'test disconnecting from namespaces': function (next) {
      var socket = create().socket
        , namespaces = 2
        , disconnections = 0;

      function finish () {
        socket.of('').disconnect();
        next();
      };

      socket.of('/a').on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.of('/a').on('connect', function () {
        socket.of('/a').disconnect();
      });

      socket.of('/a').on('disconnect', function () {
        --namespaces || finish();
      });

      socket.of('/b').on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.of('/b').on('connect', function () {
        socket.of('/b').disconnect();
      });

      socket.of('/b').on('disconnect', function () {
        --namespaces || finish();
      });
    },

    'test authorizing for namespaces': function (next) {
      var socket = create().socket

      function finish () {
        socket.of('').disconnect();
        next();
      };

      socket.of('/a')
        .on('connect_failed', function (msg) {
          next();
        })
        .on('error', function (msg) {
          throw new Error(msg || 'Received an error');
        });
    },

    'test sending json from server': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('message', function (msg) {
        msg.should().eql(3141592);
        socket.disconnect();
        next();
      });
    },

    'test sending json from client': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.json.send([1, 2, 3]);
      socket.on('message', function (msg) {
        msg.should().equal('echo');
        socket.disconnect();
        next();
      });
    },

    'test emitting an event from server': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('woot', function () {
        socket.disconnect();
        next();
      });
    },

    'test emitting an event to server': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.emit('woot');
      socket.on('echo', function () {
        socket.disconnect();
        next();
      })
    },

    'test emitting multiple events at once to the server': function (next) {
      var socket = create();

      socket.on('connect', function () {
        socket.emit('print', 'foo');
        socket.emit('print', 'bar');
      });

      socket.on('done', function () {
        socket.disconnect();
        next();
      });
    },

    'test emitting an event from server and sending back data': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('woot', function (a, fn) {
        a.should().eql(1);
        fn('test');

        socket.on('done', function () {
          socket.disconnect();
          next();
        });
      });
    },

    'test emitting an event to server and sending back data': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.emit('tobi', 1, 2, function (a) {
        a.should().eql({ hello: 'world' });
        socket.disconnect();
        next();
      });
    },

    'test encoding a payload': function (next) {
      var socket = create('/woot');

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('connect', function () {
        socket.socket.setBuffer(true);
        socket.send('ñ');
        socket.send('ñ');
        socket.send('ñ');
        socket.send('ñ');
        socket.socket.setBuffer(false);
      });

      socket.on('done', function () {
        socket.disconnect();
        next();
      });
    },

    'test sending query strings to the server': function (next) {
      var socket = create('?foo=bar');

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.on('message', function (data) {
        data.query.foo.should().eql('bar');

        socket.disconnect();
        next();
      });
    },

    'test sending newline': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.send('\n');

      socket.on('done', function () {
        socket.disconnect();
        next();
      });
    },

    'test sending unicode': function (next) {
      var socket = create();

      socket.on('error', function (msg) {
        throw new Error(msg || 'Received an error');
      });

      socket.json.send({ test: "☃" });

      socket.on('done', function () {
        socket.disconnect();
        next();
      });
    },

    'test webworker connection': function (next) {
      if (!window.Worker) {
        return next();
      }

      var worker = new Worker('/test/worker.js');
      worker.postMessage(uri());
      worker.onmessage = function (ev) {
        if ('done!' == ev.data) return next();
        throw new Error('Unexpected message: ' + ev.data);
      }
    }

  };

})(
    'undefined' == typeof module ? module = {} : module
  , 'undefined' == typeof io ? require('socket.io-client') : io
  , 'undefined' == typeof should ? require('should-browser') : should
);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions