Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the basic socket.io sample to work

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.

like image 492
user1111929 Avatar asked Feb 25 '12 18:02

user1111929


People also ask

How do I test a Socket.IO connection?

You can check the socket. connected property: var socket = io. connect(); console.

How do I run a Socket.IO in node js?

In order to do it, you need to create an index. js file and install socket.io and express. You can use the following command: touch index. js && npm install express socket.io && npm install --save-dev nodemon .

How does exactly Socket.IO work?

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.


1 Answers

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));
like image 99
bryanmac Avatar answered Oct 01 '22 10:10

bryanmac