1

I am making an app with node.js(express)+socket.io and I am wondering how I can use the socket object which is the parameter of io.on('connection',function(socket){... from outside like routes files in the routes folder

app.js

app.get('/foo/:id?', routes.foo);

routes/foo.js

exports.index = function(req, res){
  //do something with socket such as socket.join()
};

any ideas or tips would be appreciated.

edited My goal is to let a user who accesses '/foo/:id?' join the socket.io's room(https://github.com/LearnBoost/socket.io/wiki/Rooms) named id?'. So I would like to call socket.join('req.params.id') at

exports.index = function(req, res){
  //HERE
};
3
  • 2
    You can access the underlying Socket via req.connection. Commented Apr 8, 2014 at 15:35
  • 1
    @Joe Thanks for commenting.But Socket I'm talking about is the parameter of socket.io's callback.I've peeked what req.connection have though I coudln't see anything related to socket.io in it or am I missing something? Commented Apr 9, 2014 at 1:56
  • req.connection is the Node.js socket object. Not sure what is available for socket.io, sorry. Commented Apr 9, 2014 at 2:59

1 Answer 1

1
+50

You should use express.io

It's really easy to implement and you could do this:

exports.index = function(req, res){
  req.io.join(req.params.id); // The client for the request joins to the room with name req.params.id
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.