Skip to content
This repository

Realtime application framework for Node.JS, with HTML5 WebSockets and cross-browser fallbacks support.

This branch is 8 commits ahead and 390 commits behind master

Octocat-spinner-32 examples Started cleanup January 25, 2012
Octocat-spinner-32 lib Added missing options. February 26, 2012
Octocat-spinner-32 .gitignore Added decode/encode benchmarks October 11, 2011
Octocat-spinner-32 .npmignore Ignore files May 10, 2011
Octocat-spinner-32 .travis.yml Correct irc channel November 22, 2011
Octocat-spinner-32 History.md Release 0.8.7 November 05, 2011
Octocat-spinner-32 Makefile Refactored Makefile. February 26, 2012
Octocat-spinner-32 Readme.md Updated server January 31, 2012
Octocat-spinner-32 package.json Added superagent dep February 13, 2012
Readme.md

Socket.IO

Socket.IO is a Node.JS framework for making realtime applications.

It brings the best of WebSockets and other data transport mechanisms for blazing fast realtime data exchange in web browsers, mobile devices and servers.

How to Install

npm install socket.io

How to use

First, require socket.io and attach it to a HTTP/HTTPS server:

var app = express.createServer()
  , io = require('socket.io')(app);

app.listen(80);

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Finally, load it from the client side code:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

For more thorough examples, look at the examples/ directory.

Short recipes

Sending and receiving events.

Socket.IO allows you to emit and receive custom events. Besides connect, message and disconnect, you can emit custom events:

// note, io(<port>) will create a http server for you
var io = require('socket.io')(80);

io.on('connection', function (socket) {
  io.emit('this', { will: 'be received by everyone' });

  socket.on('private message', function (from, msg) {
    console.log('I received a private message by ', from, ' saying ', msg);
  });

  socket.on('disconnect', function () {
    io.emit('user disconnected');
  });
});

Storing data associated to a client

Sometimes it's necessary to store data associated with a client that's necessary for the duration of the session.

Server side

var io = require('socket.io')(80);

io.on('connection', function (socket) {
  socket.on('set nickname', function (name) {
    socket.set('nickname', name, function () { socket.emit('ready'); });
  });

  socket.on('msg', function () {
    socket.get('nickname', function (err, name) {
      console.log('Chat message by ', name);
    });
  });
});

Client side

<script>
  var socket = io.connect('http://localhost');

  socket.on('connect', function () {
    socket.emit('set nickname', confirm('What is your nickname?'));
    socket.on('ready', function () {
      console.log('Connected !');
      socket.emit('msg', confirm('What is your message?'));
    });
  });
</script>

Client side

In the client side, messages are received the same way whether they're volatile or not.

Getting acknowledgements

Sometimes, you might want to get a callback when the client confirmed the message reception.

To do this, simply pass a function as the last parameter of .emit. What's more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along:

Server side

var io = require('socket.io')(80);

io.on('connection', function (socket) {
  socket.on('ferret', function (name, fn) {
    fn('woot');
  });
});

Client side

<script>
  var socket = io.connect(); // TIP: .connect with no args does auto-discovery
  socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too!
    socket.emit('ferret', 'tobi', function (data) {
      console.log(data); // data will be 'woot'
    });
  });
</script>

Broadcasting messages

To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it.

Server side

var io = require('socket.io')(80);

io.on('connection', function (socket) {
  socket.broadcast.emit('user connected');
});

Rooms

Sometimes you want to put certain sockets in the same room, so that it's easy to broadcast to all of them together.

Think of this as built-in channels for sockets. Sockets join and leave rooms in each socket.

Server side

var io = require('socket.io')(80);

io.on('connection', function (socket) {
  socket.join('justin bieber fans');
  socket.broadcast.to('justin bieber fans').emit('new fan');
  io.to('rammstein fans').emit('new non-fan');
});

Using it just as a cross-browser WebSocket

If you just want the WebSocket semantics, you can do that too. Simply leverage send and listen on the message event:

Server side

var io = require('socket.io')(80);

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

Client side

<script>
  var socket = io.connect('http://localhost/');
  socket.on('connect', function () {
    socket.send('hi');

    socket.on('message', function (msg) {
      // my msg
    });
  });
</script>

Messaging between clients

Each client joins its own group, identified by the client id, which means you can leverage to to message a particular client:

var io = require('socket.io')(80);
io.on('connection', function (socket) {
  socket.on('salutate', function (id) {
    io.to(id)
      .emit('hi!')
      .on('disconnect', function () {
        socket.emit('friend disconnected');
      });
  });
});

Changing configuration

Configuration in socket.io is TJ-style:

Server side

var io = require('socket.io')(80);

io.configure(function () {
  io.set('transports', ['websocket', 'flashsocket', 'xhr-polling']);
});

io.configure('development', function () {
  io.set('transports', ['websocket', 'xhr-polling']);
  io.enable('log');
});

License

(The MIT License)

Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.