0

Recently, I have been experimenting with Node.js and Socket.io. Successfully, I have been able to implement these but only on localhost. Now I would like use this functionality on my dedicated server. I am hosting my server from my own home and I cannot get node and socket to run outside of localhost. The importance of this is so that I can use two different computers while testing out my site. Here is my code as follows:

app.js:

   var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

index.html:

    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:8080');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });

</script>

These are samples that were taken directly off the socket.io site. Now it works on localhost, but not externally. I cannot use it on another computer. I was advised to change

var socket = io.connect('http://localhost:8080');
to
var socket = io.connect('http://mydomain.com:8080');
, but that makes the browser throw the following errors :
 Uncaught ReferenceError: io is not defined mydomain.com/:3
    GET http://mydomain.com/socket.io/socket.io.js  mydomain.com/:1

even on my main server computer. Im using OSX server with a mac mini btw.

Any advice would be appreciated:

3 Answers3

0

It looks like you're listening on port 80 instead of 8080, and you will need to point your script block to your server, so try

<script src="http://localhost:8080/socket.io/socket.io.js"></script>

needs to be

<script src="http://mydomain.com:80/socket.io/socket.io.js"></script>

or better yet

<script src="/socket.io/socket.io.js"></script>

If that doesn't work, make sure you check for any errors under the network tab of your debugger window (presuming you're using chrome) to make sure you're loading that socket.io.js file correctly.

hexist
  • 5,151
  • 26
  • 33
0

You don't need to specify server address if it's the same where the js was loaded:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect();
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

but you need to make http://mydomain.com/socket.io/socket.io.js available for you browser.

Luman75
  • 878
  • 1
  • 11
  • 28
  • Did what you suggested and now I get this error : "GET http://mydomain.com/socket.io/1/?t=1376958569564 404 (Not Found)" Essentially, my root folder contains /socket.io/socket.io.js which solves the initial problem. – Jibran Khan Aug 20 '13 at 00:30
0

Ok. try to run this code:

filename: package.json

{
"name": "test-socketio"
, "version": "0.0.1"
, "private": true
, "dependencies": {
    "socket.io": "*"
},
"engines": {
    "node": "0.8.x"
    ,"npm" : "*"
}

}

filename: app.js

var   express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);

server.listen(80);

app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html'); 
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log("Recevied: 'my other event'");
    console.log(data);
  });
});

filename: index.html

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
    socket.on('news', function (data) {
        console.log("Received news event");
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
</script>

Then run in the code directory:

npm install

and run:

sudo node app.js

Please remember that you need to have the root rights to start listening on port 80.

Luman75
  • 878
  • 1
  • 11
  • 28
  • I tried the to attempt it again with your specifications and I received the following in terminal : `info - socket.io started warn - error raised: Error: listen EADDRINUSE` – Jibran Khan Aug 20 '13 at 23:53
  • Something else is already listening on port 80. You must turn off that service before. – Luman75 Aug 21 '13 at 06:34
  • Hi Luman 75, Im using MAC OSX Server and I have the site up and running through port 80. If I turn off port 80, given that socket can connect, I wont be able to get to my site. Would I have to do some kind of port forwarding? – Jibran Khan Aug 21 '13 at 21:04
  • what currently occupies your port 80? – Luman75 Aug 21 '13 at 21:08
  • OSX server is using my port 80 to for incoming traffic. So, I think the answer your looking for is, essentially, my site itself if routed to port 80. – Jibran Khan Aug 21 '13 at 21:36
  • @JibranKhan if your site is developed in node.js you can integrate socket.io with it. Simply in that line: `io = require('socket.io').listen(server);` server variable is your http server used for express or connect. Anyway if you are not going to integrate those parts there is no choice you need to change port on which you are listening. My example code will work out of the box if you change port in line `server.listen(80);` to something else – Luman75 Aug 22 '13 at 06:28