a sack
full of good times

Mike Cantelon
Nov 1, 2012

Hi, I'm Mike



Lots of supported transports

Can share a TCP/IP port with an HTTP server

This works with the HTTP module and Express!

Can't we just use the AJAX?

Why not?

AJAX feels a bit clunky too

Some JQuery AJAXing:

  url: '',
  type: 'POST',
  data: {
    'message': 'Who let the dogs out?'

This would be nicer:

send('question', {
  'message': 'Who let the dogs out?'
}); is nice


Sending to rooms

The basics

What's the code look like? uses "event emitters" for client and server.

socket.emit('eventType', eventData);
socket.on('eventType', function(eventData) {
  // do some things

Proof of concept app

Proof of concept app

What it'll look like

Example app's HTTP server

var http = require('http')
  , socketio = require('')
  , fs = require('fs');

fs.readFile('./index.html', function(err, fileData) {
  var server = http.createServer(function(request, response) {
    response.writeHead(200, {'Content-Type': 'text/html'});


  // we'll add code here!

Example app's server

  var io = socketio.listen(server);

  io.on('connection', function(socket) {
    var tick = 0;
    setInterval(function() {
      socket.emit('tickEvent', {'tick': tick});
    }, 1000);
    socket.on('tickReceivedEvent', function(tickData) {
      console.log('Socket %s got %d',, tickData.tick);

Example app's HTML code

<script src='/'></script>
window.onload = function() {
  var socket = io.connect();
  socket.on('tickEvent', function(tickData) {
    document.write(tickData.tick + '<br/>');

    socket.emit('tickReceivedEvent', {
      'tick': tickData.tick

Multi-room chat example

What it'll look like

What users can do

When a client connects

Joining/leaving rooms


Broadcasting/listing rooms'Lobby').emit('eventType', eventData);'Lobby').emit('eventType', eventData);

Need info about rooms?


You get data like this:

{ '': 
   [ '1493563052698187148',
     '19056402231855815978' ],
  '/Lobby': [ '1493563052698187148' ],
  '/Cellar': [ '1804042744551045346', '19056402231855815978' ] }

Need sockets in a single room?

Provide array of socket objects, not IDs, in room


When a client disconnects

socket.on('disconnect', function() {
  // free up client's nickname

Chatroom code

Other handy stuff

Acknowledgements has mechanism for this...

Client emits, defining callback:

socket.emit('eventType', eventData, function (ack) {
  console.log(ack); // do something with acknowledgement

Server receives, fires callback:

socket.on('eventType', function (eventData, fn) {
  fn('Message received!');

Multiplexing with namespaces

Specify on the server:

io.of('/chat').on('connection', function (socket) {
  // deal with socket

Then connect with the client:

Production considerations

Production configuration

Run your app set to production mode:

$ NODE_ENV=production node app.js

Configuration in is Express-style:

io.configure('production', function(){
  io.enable('browser client minification');
  io.enable('browser client etag');
  io.enable('browser client gzip');
  io.set('log level', 1);

Production configuration

The default transports might not be what you want, but you can change that.

Enabling specific transports:

io.configure('production', function(){
  io.set('transports', [


Redis can relay between processes/servers

Configure to use RedisStore:

var redis = require(''),
  RedisStore = require(''),
  pub    = redis.createClient(),
  sub    = redis.createClient(),
  client = redis.createClient();

io.set('store', new RedisStore({
  redisPub:    pub,
  redisSub:    sub,
  redisClient: client

Server can store client-specific data

Storing data:

socket.set('key', value, function () {
  // do the next thing

Retrieving data:

socket.get('key', function (err, value) {
  // do something with value


Official site:

Github project site:

Wiki (lots of stuff):


Twitter: @mcantelon