1

i'm having an issue where a user disconnects from socket.io and the instances remains active, including all event listeners.

Both examples below is partial code of my progress due to hundreds of rows. But the issue remains with this code.

app.js example:

var express = require('express'),
    app = express(),
    routes = require('./routes/index'),
    server = require('http').createServer(app);

io = require('socket.io').listen(server);

app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');

app.use('/', routes);

server.listen(3000, function() {
    console.log('listening on *:3000');
});

module.exports = app;

index.js route example:

var express = require('express'),
    router = express.Router();

router.get('/', function(req, res) {
    res.render('home', {
        title: 'Home'
    });

    setupSocket(req);
});

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

module.exports = router;

This will result in:

First connection:

connected

Second connection:

connected
connected

Third connection:

connected
connected
connected

And so on. This will continue on page reload, in a new tab, in a new browser or anything else until i restart the node server.

In my code i'm posting data from client side to a mongo database and due the the issue above, the request will post multiple copies of the same data.

So the question is, how do i prevent the instance to remain active after a user has left the page?

Jonathan Nielsen
  • 1,442
  • 1
  • 17
  • 31
  • Possible duplicate: http://stackoverflow.com/questions/5048231/force-client-disconnect-from-server-with-socket-io-and-nodejs – node_modules Mar 21 '16 at 14:25
  • Thank you, unfortunately this does not solve the issue, i run`socket.on('disconnect', function() { socket.disconnect(); });` But when i reload the page it still keep stacking 'connected' in the console – Jonathan Nielsen Mar 21 '16 at 14:29

1 Answers1

1

As you said on every connection or refresh you are getting multipleconnected strings outputs, that's because you are attaching socket.io listeners on each request. This is how you should attach the connected listener on your socket.io:

var express = require('express'),
    app = express(),
    routes = require('./routes/index'),
    server = require('http').createServer(app);

io = require('socket.io').listen(server);

app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');

app.use('/', routes);

// Your listener should be outside your routes
io.on('connection', function(socket) {
    console.log('connected')
});

server.listen(3000, function() {
    console.log('listening on *:3000');
});

module.exports = app;

And on your index.js just remove the setUp function:

var express = require('express'),
    router = express.Router();

router.get('/', function(req, res) {
    res.render('home', {
        title: 'Home'
    });
});

module.exports = router;
Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
  • How would you open a socket only on a certain URL then? – Yomo710 Jul 25 '17 at 15:15
  • Why would you want that? You want your socket server running every time – Fabio Antunes Jul 25 '17 at 15:18
  • My socket starts another function, so I only wanted it to register a socket connection on a certain URL. I was putting it under the URL request block but am having the same problem as OP. – Yomo710 Jul 25 '17 at 15:27
  • But that's front end specific. On your website whenever you are on a certain URL you create the connection to the socket server. Your socket server has nothing to do with URLs, sockets are streams of events. – Fabio Antunes Jul 25 '17 at 15:42
  • Ahh okay, I think I was misunderstanding. So on the front-end I'll just emit a message saying the user visited that page that my socket can handle, correct? – Yomo710 Jul 25 '17 at 15:44
  • Yep on the front-end you choose when to start the connection to your socket server, and then just send the message, make sure you create a Handler on the Back end for listening to that specific message. If you find any difficulties, just create a new question on stackoverflow – Fabio Antunes Jul 25 '17 at 15:52
  • Perfect. Thank you! – Yomo710 Jul 25 '17 at 15:54