1

I have the following code on my server

var socket  = require( 'socket.io' );
var express = require('express');
var app     = express();
var server  = require('http').createServer(app);
var io      = socket.listen( server );
var port    = process.env.PORT || 3000;

server.listen(port, function () {
  console.log('Server listening at port %d', port);
});


io.on('connection', function (socket) {

  socket.on( 'new_count_message', function( data ) {
    io.sockets.emit( 'new_count_message', { 
     new_count_message: data.new_count_message

    });
  });

  socket.on( 'update_count_message', function( data ) {
    io.sockets.emit( 'update_count_message', {
     update_count_message: data.update_count_message 
    });
  });

  socket.on( 'new_message', function( data ) {
    io.sockets.emit( 'new_message', {
     name: data.name,
     email: data.email,
     subject: data.subject,
     created_at: data.created_at,
     id: data.id
    });
  });

  
});

But instead of showing "update_count_message" in the console, the console logs a continuous stream of polls.

 
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081140-15
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850080247-12
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850080252-13
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081252-16
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081290-17
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081351-18
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081416-19
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081474-20
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081532-21
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081576-22
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081651-23

how can I solve this? I do not want to receive a loop in the browser.

pah
  • 4,700
  • 6
  • 28
  • 37

1 Answers1

0

The browser/socket is not connecting to NodeJS. NodeJS is either not running, or you are attempting to connect to the wrong port - in your case it looks like it is trying to connect to port 80. If you already have Apache running on port 80, you can't run both NodeJS and Apache on the same port unless you setup a reverse proxy in your httpd.conf.

You can follow the debug instructions here to see what more descriptive errors SocketIO might have for you:

http://socket.io/docs/logging-and-debugging/

You could also go to this URL in the browser and see what page it is trying to connect to:

http://localhost/socket.io/

Clay
  • 4,700
  • 3
  • 33
  • 49
  • when I access the page http://localhost:3000/socket.io/ appears as follows: {"code":0,"message":"Transport unknown"}. – Wesley Igor Nov 30 '15 at 02:40
  • "GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081651-23" <-- that says it is connecting to port 80 not port 3000. Notice how it does not have a 3000. You may need to specify port 3000 in your client. – Clay Nov 30 '15 at 02:42
  • I call the script like this: something wrong ? – Wesley Igor Nov 30 '15 at 02:47
  • what does your io.connect() look like on the client/browser side? For example, mine looks like this: `var socket = io.connect('http://localhost:3000')` – Clay Nov 30 '15 at 02:49
  • it looks like your client is connecting to `http://localhost:80` not `http://locahost:3000` so it is not ever connecting to NodeJS and is going into the infinite loop. – Clay Nov 30 '15 at 02:55
  • Open the debug console in Chrome and it'll like something like "connection refused" – Clay Nov 30 '15 at 03:00
  • how do you call io.connect() in the client side? do you pass in a port or anything? change your io.connect to look like this - `io.connect('http://localhost:3000')` – Clay Nov 30 '15 at 03:14
  • 1
    Clayton, thank you, I actually forgot to put the port on the client side. – Wesley Igor Nov 30 '15 at 03:36