Wynn Netherland changelog.com/posts

Socket.IO - multi-transport socket server for Node.js

Socket.IO from RosePad is a web sockets server for Node.js that supports a number of transport options including:

  • WebSocket (with Flash policy support)
  • Server-Sent Events
  • XHR Polling
  • XHR Multipart Streaming
  • Forever Iframe

To create a socket, create by creating your server and instructing Socket.IO to listen:

var http = require('http'), 
        io = require('./socket.io/socket.io.js'),

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, I choose you
io.listen(server);

In the browser, you’ll need the Socket.IO client. First include the minified version and configure Socket.IO with the path to your server:

<script src="/path/to/socket.io.min.js">
<script>
    io.setPath('/path/to/socket.io/');
</script>

Once configured, you can send data and wire up callbacks when Socket.IO receives data from the server:

socket = new io.Socket('localhost');
socket.connect();
socket.send('some data');
socket.addEvent('message', function(data){
    alert('got some data' + data);
});

[Source on GitHub] [Client on GitHub]


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00