Can socket.io work across domains?

I have a socket.io implementation, and I'm trying to make it work across domains but it appears it can't. Here's my exact use case:

  • I have a node.js server running socket.io.
  • I have a JS / HTML5 game that I want to be able to host from anywhere, which has the client socket.io code and tries to connect to the server.
  • I have the same JS code running on mobile devices via SpiderMonkey (this is one of the main reasons I need to run this from any domain).

I find that I simply cannot connect to the socket unless I am serving the JS code from my node.js server to a browser. If I simply open the HTML file on my disk from my browser, for example, it will not work.

Server code:

io.on( "connection", function( socket )
    {
        this.socket = socket;
        this.socket.on( "echo", function( str ){ this.socket.emit( "message", str ); }.bind( this ) );
    }.bind( this ) );

Client code:

this.socket = io.connect( ip );
this.socket.on("message", function( str ) { console.log( str ); } );

And I can't even connect, I get my print out that it's trying to connect, but it never succeeds and then starts throwing ping errors. So, is this even possible? Or must I do it with long polling or something?

The error:

[Error] Failed to load resource: A server with the specified hostname could not be found. (socket.io, line 0)

EXTRA INFO: This specifically will not work if I am trying to connect from an HTML file opened from disk using file:///. I am going to try across different machines on the same network...

Yes, Socket.IO works cross-domain just fine. The actual Socket.IO library can/should be loaded from the server hosting it.

EXTRA INFO: This specifically will not work if I am trying to connect from an HTML file opened from disk using file:///.

Don't do that. You run into all sorts of weird issues when loading files off disk vs. over HTTP.

Failed to load resource: A server with the specified hostname could not be found.

This usually implies that whatever hostname you're trying to connect to isn't resolvable, indicating a DNS problem or that you typoed your hostname. In any case, your code example doesn't show the relevant part where the problem may be. Use your browser's developer tools to first determine that everything is loading correctly. Then, verify the address of what you're trying to connect to.