| CARVIEW |
Socket.IO 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:
var socket = new io.Socket();
socket.on('connect', function(){
socket.send('hi!');
})
socket.on('message', function(data){
alert(data);
})
socket.on('disconnect', function(){}) Under the hoods, Socket.IO will use feature detection to decide if the connection will be established with WebSocket, AJAX long polling, etc (see supported transports), making creating realtime apps that work everywhere a snap.
1. Install `Socket.IO` with npm
npm install socket.io
Or check out the repository with git and all its submodules
git clone git://github.com/LearnBoost/Socket.IO-node.git socket.io --recursive
Then require() it and attach it to a node http.Server
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.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello world</h1>');
});
server.listen(80);
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
// new client is here!
client.on('message', function(){ … })
client.on('disconnect', function(){ … })
}); On the client side
<script src="https://{node_server_url}/socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket({node_server_url});
socket.connect();
socket.on('connect', function(){ … })
socket.on('message', function(){ … })
socket.on('disconnect', function(){ … })
</script> If you won't leverage Node.JS to serve your files, make sure to set the global `WEB_SOCKET_SWF_LOCATION` to the location of the WebSocketMain.swf file.
In order to provide realtime connectivity on every browser, Socket.IO selects the most capable transport at runtime, without it affecting the API.
- WebSocket
- Adobe® Flash® Socket
- AJAX long polling
- AJAX multipart streaming
- Forever Iframe
- JSONP Polling
- Internet Explorer 5.5 - 8
- Safari 3 - 5
- Google Chrome 4 - 6
- Firefox 3-4
- Opera 10.61
- iPhone Safari
- iPad Safari
- Android WebKit
- WebOs WebKit
- MapRejuice
- Inflatable Chum
- Push Relay
- Lost & Found
- Speedo
- DrawTogether
- Streamie
- Alfamegle
- GabberTalk2010 Node KO winner
- Serrano2010 Node KO winner
- Socket-Chat
- Swarmation2010 Node KO winner
- Gunpixel
- Giant Robots2010 Node KO winner
- Massivetris
- Fragnut
- Space Tweet
- Lazeroids
Besides the official Node.JS server, several implementations have been started for other languages / frameworks that are compatible with the Socket.IO client:
Absolutely, on every browser!
Flash is absolutely not requiredfor 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.
If you're not relying on Node.JS serving Socket.IO clientside JavaScript files, make sure you set the `WEB_SOCKET_SWF_LOCATION` right after including socket.io.js with the location of the WebSocketMain.swf
This is required in order for Socket.IO to find the .swf file required for Flash WebSocket.
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.
This is akin to jQuery's decision of creating a feature-rich and simple $.ajax API as opposed to normalizing XMLHttpRequest.
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.
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.
This is an example of express and Socket.IO:
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
var io = require('socket.io');
var socket = io.listen(app);
socket.on('connection', function(){
// …
}) Socket.IO by Guillermo Rauch at gradebook LearnBoost Labs. Released under the MIT license - Copyright LearnBoost 2010