E
E
Egor2015-02-01 20:57:27
JavaScript
Egor, 2015-02-01 20:57:27

Express 4 + socket.io + connect-mongo. How to get data from session in socket events?

Greetings.
I store sessions in base. For this I use 'connect-mongo'.
How to get data from session inside socket event? For example, when connecting a socket, you need to take the user's login from the session and send an event to the client.

io.on('connection', function(socket){
    var username = /* тут нужно получить данные из сессии*/
    socket.emit('welcome','Привет, ' + username);
});

The problem with sending the login is not the only one, so there is no need to offer alternative solutions for passing the user's login when connecting. Interested in the possibility of obtaining data from the session.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor, 2015-02-02
@ByKraB

stackoverflow.com/questions/25532692/how-to-share-...
Super simple solution. It's strange that it's hard to find.
The example in the link uses Redis, but it also works for mongoDB.

Y
Yuri Shikanov, 2015-02-01
@dizballanze

Here it is described how to do it for Express 3, and here is an example. I think for the 4th version it will not be a problem to finalize.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question