Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use socket.io to communicate with another server when the actual page is being served by a localhost server?

I'm serving my page through localhost (XAMPP, Apache), and on my friend's physical server I run a node.js server that is used for communication with the page (a game).

This is the node.js server code:

var io = require('socket.io').listen(1235);

io.sockets.on('connection', function (socket)
{
    socket.on("start", function (data)
    {
        console.log(data);
    });
});

It runs without any errors, but I don't know how to include the socket.io code into my webpage! How do I do that?

like image 931
corazza Avatar asked May 03 '12 15:05

corazza


People also ask

How does Socket.IO work internally?

The bidirectional channel between the Socket.IO server (Node. js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

How many Socket.IO connections can a server handle?

As we saw in the performance section of this article, a Socket.IO server can sustain 10,000 concurrent connections.


2 Answers

Include a script tag in your page:

<script src="http://[YOUR IP]:1235/socket.io/socket.io.js">

And it will be served by your node.js server.

Apart from that, you can just follow the examples on socket.io, e.g.:

var socket = io.connect("http://[YOUR IP]:1235");

socket.emit("start", "LET'S GO!");
like image 77
Linus Thiel Avatar answered Oct 25 '22 23:10

Linus Thiel


2 Options. Per the documentation, you can do a JavaScript src pointing at your node server:

<script src="http://url.to.node.com/socket.io/socket.io.js"></script>

Or you can include it manually, grabbing it from the Git repo at https://github.com/LearnBoost/socket.io-client/blob/master/dist/socket.io.js

like image 42
Matt Avatar answered Oct 25 '22 22:10

Matt