mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
40 lines
9.1 KiB
HTML
40 lines
9.1 KiB
HTML
<!DOCTYPE html><html><head><title>Socket.IO: the cross-browser WebSocket for realtime apps.</title><link href="/css/main.css" rel="stylesheet" media="all"><link href="/support/prettify/prettify.css" rel="stylesheet" media="all"><script src="/socket.io/socket.io.js"></script><script src="/support/prettify/prettify.js"></script></head><body onload="prettyPrint()"><div id="wrap"><div id="header"><div class="subtext">Introducing</div><a href="/" class="logo">Socket.IO</a><a href="http://github.com/learnboost/Socket.IO-node/tree/0.6" class="download"><span class="version">v</span><span class="number">.6</span></a></div><div id="content"><div class="section"><div class="title">Socket.IO</div><p><strong>Socket.IO</strong> aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. The client side API looks like this: </p><pre class="prettyprint">var socket = new io.Socket();
|
|
socket.on('connect', function(){
|
|
socket.send('hi!');
|
|
})
|
|
socket.on('message', function(data){
|
|
alert(data);
|
|
}) socket.on('disconnect', function(){}) </pre><p>Under the hoods, Socket.IO will use <b>feature detection </b>to decide if the connection will be established with WebSocket, AJAX long polling, etc (see <a href="#transports">supported transports</a>), making creating realtime apps that work everywhere a snap. </p></div><div class="section"><div class="title">How to use (Node.JS)</div><p>1. Install `Socket.IO` with <a href="http://npmjs.org/">npm</a></p><pre class="prettyprint">npm install socket.io </pre><p>Or check out the repository with git and all its submodules</p><pre class="prettyprint">git clone git://github.com/LearnBoost/Socket.IO-node.git socket.io --recursive </pre><p>Then require() it and attach it to a node <a href="http://nodejs.org/api.html#http-server-157">http.Server</a></p><pre class="prettyprint">var http = require('http'),
|
|
io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io')
|
|
server = http.createServer(function(req, res){
|
|
// your normal server code
|
|
res.writeHeader(200, {'Content-Type': 'text/html'});
|
|
res.writeBody('<h1>Hello world</h1>');
|
|
res.finish();
|
|
});
|
|
|
|
// socket.io
|
|
var socket = io.listen(server);
|
|
socket.on('connection', function(client){
|
|
// new client is here!
|
|
client.on('message', function(){ … })
|
|
client.on('disconnect', function(){ … })
|
|
}); </pre><p>On the client side</p><pre class="prettyprint">
|
|
<script src="http://{node_server_url}/socket.io/socket.io.js"></script>
|
|
<script>
|
|
var socket = new io.Socket({node_server_url});
|
|
socket.on('connect', function(){ … })
|
|
socket.on('message', function(){ … })
|
|
socket.on('disconnect', function(){ … })
|
|
</script> </pre><p class="note">If you won't leverage Node.JS to serve your files, make sure to call `io.setPath()` with the path to your client side </p></div><div class="section"><div class="title">Supported transports</div><p>In order to provide realtime connectivity on every browser, Socket.IO selects the most capable transport at runtime, without it affecting the API. </p><ul><li>WebSocket</li><li>Adobe® Flash® Socket</li><li>AJAX long polling</li><li>AJAX multipart streaming</li><li>Forever Iframe</li><li>JSONP Polling</li></ul></div><div class="section"><div class="title">Supported browsers</div><div class="subtitle">Desktop</div><ul><li>Internet Explorer 5.5 - 8</li><li>Safari 3 - 5</li><li>Google Chrome 4 - 6</li><li>Firefox 3-4</li><li>Opera 10.61</li></ul><div class="subtitle">Mobile</div><ul><li>iPhone Safari</li><li>iPad Safari</li><li>Android WebKit</li><li>WebOs WebKit</li></ul></div><div class="section"><div class="title">In the wild</div><div class="subtitle">Applications</div><ul><li><a href="http://maprejuice.com/">MapRejuice</a></li><li><a href="http://inflatable-chum.no.de/">Inflatable Chum</a></li><li><a href="http://braintree.no.de/">Push Relay</a></li><li><a href="http://nodty.no.de/">Lost & Found</a></li><li><a href="http://speedo.no.de/">Speedo</a></li><li><a href="http://github.com/saikat/drawtogether">DrawTogether</a></li><li><a href="http://github.com/cramforce/streamie">Streamie</a></li><li><a href="http://github.com/mkilling/alfamegle">Alfamegle</a></li><li><a href="http://explorer-sox.no.de">GabberTalk</a><span>2010 Node KO winner</span></li><li><a href="http://starcraft2destroyedmymarrage.no.de/">Serrano</a><span>2010 Node KO winner</span></li><li><a href="http://github.com/arunjitsingh/socket-chat">Socket-Chat</a></li></ul><div class="subtitle">Games</div><ul><li><a href="http://swarmation.com">Swarmation</a><span>2010 Node KO winner</span></li><li><a href="http://demiox-boiko.no.de/">Gunpixel</a></li><li><a href="http://piston-hurricane.no.de/">Giant Robots</a><span>2010 Node KO winner</span></li><li><a href="http://virtual-design.no.de/">Massivetris</a></li><li><a href="http://fragnut.me/">Fragnut</a></li><li><a href="http://github.com/jacobthornton/space-tweet">Space Tweet</a></li><li><a href="http://github.com/gerad/lazeroids-node/">Lazeroids</a></li></ul><div class="subtitle">Frameworks / Components</div><ul><li><a href="http://github.com/substack/dnode">DNode</a></li><li><a href="http://github.com/bnoguchi/Socket.IO-connect">Socket.IO-connect</a></li><li><a href="http://github.com/saikat/socket-logger/">Socket-logger</a></li><li><a href="http://github.com/maccman/juggernaut">Juggernaut</a></li><li><a href="http://github.com/jamescarr/nodejs-amqp-example">AMPQ + Socket.IO</a></li></ul></div><div class="section"><div class="title">Server Implementations</div><p>Besides the official <a href="http://github.com/learnboost/socket.io-node">Node.JS server</a>, several implementations have been started for other languages / frameworks that are compatible with the <a href="http://github.com/learnboost/socket.io">Socket.IO client</a>: </p><ul><li><a href="http://github.com/madari/go-socket.io">Google GO</a></li><li><a href="http://github.com/markjeee/Socket.IO-rack">Rack</a></li></ul></div><div class="section"><div class="title">Resources</div><div class="subtitle">Articles</div><ul><li><a href="http://nodeknockout.posterous.com/countdown-to-knockout-post-9-websockets-every">Socket.IO and the history of the realtime web - Guillermo Rauch, Node Knockout Blog</a></li><li><a href="http://jeffkreeftmeijer.com/2010/things-i-learned-from-my-node.js-experiment/">Node.JS WebSocket experiment - Jeff Kreeftmeijer</a></li><li><a href="http://substack.net/posts/85e1bd/DNode-Asynchronous-Remote-Method-Invocation-for-Node-js-and-the-Browser">Asynchronous remote method invocation with DNode - James Halliday, SubStack blog</a></li></ul></div><div class="section"><div class="title">FAQ</div><div class="subtitle">Does Socket.IO support cross-domain connections?</div><p>Absolutely, on every browser!</p><div class="subtitle">Why Flash?</div><p>Flash is absolutely <b>not required</b>for Socket.IO to function. If Flash is available, it'll be leveraged, as it provides almost the same capabilities as WebSocket. If it's not, the best next transport will be chosen. </p><div class="subtitle">I want to host the Socket.IO client myself</div><p>If you're not relying on Node.JS serving Socket.IO clientside JavaScript files, make sure you call io.setPath right after including socket.io.js with the location of where you cloned the Socket.IO client repository: </p><pre class="prettyprint"><script src="/the/path/to/socket.io/socket.io.js"></script>
|
|
<script>io.setPath('/the/path/to/socket.io')</script> </pre><p>This is required in order for Socket.IO to find the .swf file required for Flash WebSocket.</p><div class="subtitle">Why not just call it `WebSocket` if the actual WebSocket is not present and mimick its API?</div><p>Socket.IO does more than WebSocket, even if WebSocket is selected as the transport and the user is browsing your website with an ultra modern browser. Certain features like heartbeats, timeouts and disconnection support are vital to realtime applications but are not provided by the WebSocket API out of the box. </p><p>This is akin to jQuery's decision of creating a feature-rich and simple $.ajax API as opposed to normalizing XMLHttpRequest.</p><p>Creating our own namespace io.Socket helps also avoid confusion and also makes it naturally extensible. You can add your own shortcut methods and alter the prototype without altering the original standard API. </p><div class="subtitle">Can I use Socket.IO with express / connect ?</div><p>Sure! Socket.IO only needs access to the http.Server instance of Node. Since all frameworks build on top of that API, Socket.IO is compatible with any framework out of the box. </p><p>This is an example of express and Socket.IO:</p><pre class="prettyprint">var app = express.createServer();
|
|
app.get('/', function(req, res){
|
|
res.send('Hello World');
|
|
});
|
|
app.listen(3000);
|
|
|
|
var io = require('socket.io');
|
|
io.listen(app);
|
|
io.on('connection', function(){
|
|
// …
|
|
}) </pre></div></div><div id="footer"><p>Socket.IO by <a href="http://devthought">Guillermo Rauch</a> at <a href="http://labs.learnboost.com">LearnBoost Labs</a>. Released under the MIT license - Copyright <a href="http://learnboost.com">LearnBoost 2010</a></p></div></div></body></html> |