3

I'm trying to seperate my socket.io code in my app to it's own file like the answer in this thread: Separating file server and socket.io logic in node.js

However, the socket.io code never runs and will not start to listen and I don't really understand why:

//index.js
kraken.create(app).listen(function (err, server) {
        if (err) {
            console.error(err);
        }
        var io = require('./lib/socket').listen(server);
    });

//socket.js
var socketio = require('socket.io')

module.exports.listen = function(app) {
    return function (req, res) {
        io = socketio.listen(app);

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

            socket.on('disconnect', function () {
                if (socket.uid !== undefined) {
                    // do some stuff
                }
            });
        });

        return io;
    }
};

My best guess is that the req and res objects doesn't exist since it's not a request? The problem is that I really need to use the req object since I need to access the session during connect. If this is the issue, how can achieve that?

Thanks in advance for your sage advice and better wisdom.

EDIT: I have looked at https://github.com/jfromaniello/passport.socketio which is kinda exactly what I need, however I don't know what my session key or secret is. How can I know that with passport?

Community
  • 1
  • 1
Ms01
  • 4,420
  • 9
  • 48
  • 80

1 Answers1

3

Your listen function is just returning another function and not actually triggering the code to setup socketio to listen.

Try:

module.exports.listen = function(app) {
    io = socketio.listen(app);

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

        socket.on('disconnect', function () {
            if (socket.uid !== undefined) {
                // do some stuff
            }
        });
    });

    return io;
};

Here's another example: https://github.com/paypal/kraken-js/issues/39

ysalmi
  • 529
  • 4
  • 16
  • Yeah I know, I removed it the return statement and it worked. However, I want to associate the socket with the logged in user which is impossible right now since the socket.io stuff doesn't have access to the session? This is my main issue, the socket stuff is working as intended. :) – Ms01 Mar 01 '14 at 23:34
  • 1
    OK, that wasn't very clear from your original question. Check out the link you posted about passport, they have an example, and further down they detail how to setup the key and secret. Or check out this related post: http://stackoverflow.com/questions/13095418/how-to-use-passport-with-express-and-socket-io – ysalmi Mar 02 '14 at 10:54
  • Sorry for that. I looked at the examples, but I still have a hard time translating it to krakenjs. Should I require express and then initiate the session? Also I have a hard time understanding why I need the express session, doesn't passport operate on it's own session? – Ms01 Mar 02 '14 at 19:58
  • 1
    I think you're asking a different question at this point. I don't know enough about passport or krakenjs to answer. It does seem like passport is opening up its own session, and then you can inject the authenticated user into the res structure. Here's a tutorial covering kraken with passport, maybe it can help. I would focus on keeping things simple and adding one piece of tech at a tie. – ysalmi Mar 02 '14 at 21:19
  • Forgot to link the actual tutorial for krakenJS with Passport: https://github.com/lmarkus/Kraken_Example_Passport – ysalmi Mar 07 '14 at 13:49