official url http://socket.io/download/ http://socket.io/get-started/ https://www.npmjs.com/package/socket.io https://github.com/Automattic/socket.io https://github.com/Automattic/engine.io https://github.com/nswbmw/N-chat
basic use npm install socket.io client: <script src="http://localhost:3000/socket.io/socket.io.js"></script>
server side - io.on socket.emit socket.on var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }); app.get('/jquery-1.11.1.js', function(req, res){ res.sendFile(__dirname + '/jquery-1.11.1.js'); }); app.get('/socket.io-1.2.0.js', function(req, res){ res.sendFile(__dirname + '/socket.io-1.2.0.js'); }); io.on('connection', function(socket){ console.log('a user connected'); socket.on('chat message', function(msg){ io.emit('chat message', msg); console.log("msg: " + msg); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); });
Client (index.html) <body> <ul id="messages"></ul> <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> <script src="http://localhost:3000/socket.io-1.2.0.js"></script> <script src="http://localhost:3000/jquery-1.11.1.js"></script> <script> var socket = io(); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); </script> </body> <script src="/socket.io/socket.io.js"></script> <script> var socket = io('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>
namespace server side var io = require('socket.io')(80); var chat = io .of('/chat') .on('connection', function (socket) { socket.emit('a message', { that: 'only' , '/chat': 'will get' }); chat.emit('a message', { everyone: 'in' , '/chat': 'will get' }); }); var news = io .of('/news') .on('connection', function (socket) { socket.emit('item', { news: 'item' }); });
namespace client side <script> var chat = io.connect('http://localhost/chat') , news = io.connect('http://localhost/news'); chat.on('connect', function () { chat.emit('hi!'); }); news.on('news', function () { news.emit('woot'); }); </script>
volatile messages var io = require('socket.io')(80); io.on('connection', function (socket) { var tweets = setInterval(function () { getBieberTweet(function (tweet) { socket.volatile.emit('bieber tweet', tweet); }); }, 100); socket.on('disconnect', function () { clearInterval(tweets); }); });
callback Server (app.js) var io = require('socket.io')(80); io.on('connection', function (socket) { socket.on('ferret', function (name, fn) { fn('woot'); }); }); Client (index.html) <script> var socket = io(); // TIP: io() 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 var io = require('socket.io')(80); io.on('connection', function (socket) { socket.broadcast.emit('user connected'); });