I know that I can use
function(req, res) {
req.session
}
using express. However I need to access the session outside of the response function. How would I go about doing that?
I'm using socket.io to pass information for adding posts and comments. So when I receive the socket.io message on the server-side, I need to verify the person posting the information by using the session. However since this is being done via socket.io there is no req/res.
Using socket.io, I've done this in a simple way. I assume you have an object for your application let's say MrBojangle, for mine it's called Shished:
Then on your authorization method, I'm using MongoDB for session storage:
Then before saving a record in the model, you can do:
model._author = shished.getHandshakeValue( socket, 'userId' );
I think I have a different answer.
code:
then you should define a session key at req,just like :
code:
finally,you can get it this way:
code:
I had test it yesterday, it worked well.
Assuming your socket.io code looks kinda like this:
The request is
client.request
as shown in the example above.Edit: As a separate thing, maybe this would help: https://github.com/aviddiviner/Socket.IO-sessions
I believe checking
socket.handshake
should get you the session:When the client establishes a socket connection with your socket.io server, the client sends a WebSocket handshake request. What I'm doing above is grabbing the session ID from the handshake.