16

I'm having trouble even getting the very basic socket.io sample to run. For example the first example on the welcome page of their site:

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

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

on the server side and

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

on the client side. If I save the server-side in a host.js file, and the client-side in a client.htm file, and I run npm host.js, I get

   info  - socket.io started
   warn  - error raised: Error: listen EADDRINUSE

which is already not really expected. Then, for the client.htm (or at least that's what I think that I'm supposed to do with it -- pasting it in a client.htm file), I only get a blank screen. Not very surprising, since it starts by including a nonexisting file /socket.io/socket.io.js, but even changing this to host.js (which I assume it is supposed to be) doesn't change the fact that I only get a blank screen...

I'm clueless.

bryanmac
  • 38,941
  • 11
  • 91
  • 99
user1111929
  • 6,050
  • 9
  • 43
  • 73

1 Answers1

16

EADDRINUSE means that address is already in use so it can't get the socket. Is something else already running on port 80 on your machine? 80 is commonly used by web servers.

You can also try it on some other port.

The reason you see a blank file is it doesn't connect to the node server (since it couldn't get the socket) so the on news event will never get called. It might even connecting to the socket of whatever else is running on 80 which will never emit that event :)

After you solve the port conflict, when you run the server, it should just say:

info - socket.io started

and now it's waiting for connections.

Make sure you update the htm line to your port. For example, if 81:

var socket = io.connect('http://localhost:81'); // note the :81

EDIT: I just tried it out and in the htm file I had to set the relative path to the socket.io.js file. After installing it via npm it was here in my directory.

<script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>

Make sure that path is relative to the htm file (doesn't start with a /). Here's how I found out where mine was:

find . -name 'socket.io.js'

On Win: dir socket.io.js /s

You should also run the host with (on *nix you may need sudo in front):

node host.js

Last thing I did when trying the sample was changing a couple lines in the htm file to this so I could see an alert message box when the event happened:

socket.on('news', function (data) {
   alert(JSON.stringify(data));
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • 1
    Does it matter what port? I tried 81 and now it just hangs when running the host side, i.e. it says `info - socket.io started` but it doesn't return to the command line. The .htm page is still blank, so this does not seem to solve the problem. – user1111929 Feb 25 '12 at 21:09
  • 3
    It solved the EADDRINUSE error which was your first issue. Yes it matters. It's also not hanging - the server is running waiting for communications. So, now you've solved the server side half of the problem. – bryanmac Feb 25 '12 at 21:24
  • for the client side of the problem, did you update the htm page to connect to http://localhost:81 (not the :81)? – bryanmac Feb 25 '12 at 21:24
  • No, I didn't add the :81, and now :81 seems in use (apparently this thing never stops listening?) but adding :82 in and chaning the host to :82 as well doesn't change the problem. A blank screen on the client.htm (which has a :82 after localhost now) and the host still hangs on "socket.io started", it does not notify me of any incoming connections. – user1111929 Feb 25 '12 at 23:13
  • Also, did I do well in changing `/socket.io/socket.io.js` to just `host.js`? It doesn't really seem correct that I just include the host script from the client script, but I just don't see what else I should be including... where/how to get a `/socket.io/socket.io.js` file? Just reverting it to the default `/socket.io/socket.io.js` doesn't change the problem either in any case. – user1111929 Feb 25 '12 at 23:29
  • yeah - you have to stop the server process with ctrl-c. I just got it working on my machine. See my last edit to the post to see what else I had to do. – bryanmac Feb 25 '12 at 23:46
  • I changed the port in the host.js to 83 now and have this for the client.html: http://88.198.109.15/socket.io/client.htm but the problem persists. Nothing happens on the command line: it keeps waiting for connectons and it doesn't notice that client.htm is opened. Am I still doing anything wrong here? My client.htm is in the link, my host.js is the original with port 83 instead of 80. – user1111929 Feb 26 '12 at 00:16
  • did you find the socket.io.js and ensure the relative path in the htm file to it is correct? Also, see my last edit - I changed it to an alert so when it works, it pops up. – bryanmac Feb 26 '12 at 00:30
  • The relative path is correct -- as you can easily see by going to http://88.198.109.15/socket.io/client.htm and clicking on it in the source code. I however get no popup, so the alert() doesn't get triggered. So I'm afraid there is another annoying problem somewhere. :( Also, it might be relevant to mention (but probably not) that I don't need sudo to run node, perhaps because I installed it with root access. – user1111929 Feb 26 '12 at 00:59
  • 1
    I went to that url but 'localhost' on my machine is not your machine that is running the node server. localhost is my machine. – bryanmac Feb 26 '12 at 02:51
  • 1
    Ahh, so it had to be http://88.198.109.15:83 instead of http://localhost:83. I already wondered how this localhost thing could be resolved correctly by the client. Good, now it works, huge thanks! – user1111929 Feb 26 '12 at 10:53