2

I am working on a node.js project that I am leveraging Socket.IO in, and am having an issue getting my head around a scoping issue. Here is what I am trying to do:

var io = require('socket.io').listen(80);
session_manager = require('./includes/session_manager');

// client joins the socket server
io.sockets.on('connection', function(client) {
    client.on('X.Session.join', function(session_id, client) {
        session_manager.joinSession(session_id, function(err, session) {
            // do whatever
        });
    });
    // BRING IN MORE LISTENERS/EMITTERS?
    require('someModuleIBuild');
});

As you can see I am basically setting up the initial connection, joining a session via a managing class (so I know who to emit to for which session) and then I am trying to dynamically bring in some custom stuff that ALSO is going to be emitting and listening via the socket connection.

So how do I reference this current connection from within the confines of my custom modules? All the examples I have seen have all the "on" and "emit" functions in one file, which seems like it could get out of control pretty quickly.

I am possibly over-thinking/over-complicating this (this is my first node.js project, first socket-based project, first mostly-javascript project....etc) but any help would be appreciated.

1
  • I just ran into the same issue yesterday. The solution I'm currently playing with is having my custom module emit events and then having socket.io listen for any such events.. but I'm not sure if that's the best way to go about it :\ Commented Aug 11, 2011 at 13:43

1 Answer 1

1

create your modules like this and you can pass the client into the module

module.exports = function(client) {
    client.on("whatever", function () {

    });

    client.on("whenever", function (data) {

    });
};

and then do the require like this

require('someModuleIBuild')(client);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.