0

i am pretty new to Node.js . I am using socket.io to build up a real time feed application.

i am using http://socket.io/get-started/chat/ to get started with my application. in combination with express

Following is my server code

/**
 * Module dependencies.
 */
var express = require('express');
var logger = require('morgan');
var routes = require('./routes');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

// log requests
app.use(logger('dev'));
app.use('/rest', routes.userInfo, routes.matchList, routes.teamList,
        routes.completedMatchList);

io.on('connection', function(socket){
      console.log('a user connected');
    });

app.listen(3006);

console.log('listening on port 3006');

Then i have an index.html inside my web folder of my application.

I am just using plain code to intitalize the client socket as below

 <script src="/node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>     
<script>
  var socket = io();
</script>

I started my server and its work well But even after the client page loads and is served, connection event is not listened and the code is not printing in the console "user is connected".

Just to add a little bit, i use an application router in middle which starts at port 5000.

i checked in the chrome console, it is constantly reporting errors like

http://localhost:5000/socket.io/?EIO=3&transport=polling&t=LF2viiR

http://localhost:5000/socket.io/?EIO=3&transport=polling&t=LF2vj0v

Snapshot my chrome console with errors enter image description here

cheers,

Saurav

saurav
  • 5,388
  • 10
  • 56
  • 101

3 Answers3

0

In this case you can't use app.listen() because then you're not listening with the HTTP instance that Socket.io is using.

You're passing your express instance app to your HTTP instance http when created. Thus app is used as the base for http. Then you're passing http to socket.io when creating io.

var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

What you need to do is listen with your http variable since that is the server you're instantiating io with. Change your app.listen() to http.listen().

peteb
  • 18,552
  • 9
  • 50
  • 62
  • thans for your inputs...did that but not its giving the same results – saurav Mar 29 '16 at 15:35
  • @saurav what does "not its giving the same results" mean? What kind of results are you getting when doing that? – peteb Mar 29 '16 at 15:37
  • i meant its giving the same results as in chrome reporting 404 errors when socket.io is trying to poll and server not responding to connection events – saurav Mar 29 '16 at 15:41
0

At first install socket.io by npm npm install socket.io --save,

Node Program(Backend):

Instead Of:

app.listen(3006);

Change into:

  http.listen(port, function(err) {
    if (err) {
        console.log(err);
    } else {
        console.log("Listening on port 8080");
    }
});

Frontend:

If your Node program and Frontend program run in a same port in localhost then,

<script src="/socket.io/socket.io.js"></script>     
<script>
  var socket = io();
</script>

And Node program and Frontend program run in two different port.

<script src="http://localhost:5000/"></script>

<script>
  var socket = io("http://localhost:5000/socket.io/socket.io.js");
</script>

Hope it will work if node program is ok.

Zahidur Rahman
  • 1,688
  • 2
  • 20
  • 30
0

Thanks a lot for your inputs and helps.

Couple of things i did which helped me to solve the problem.

(a) i used io.connect("url") in place of io().

(b) Second ordering of initialization of variables and dependencies in server's index.js

Both of the solutions are inspired by this link

SocketIO- GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1418187395022-0 404 (Not Found)

cheers,

Saurav

Community
  • 1
  • 1
saurav
  • 5,388
  • 10
  • 56
  • 101