0

I am trying to access data of socket.id outside of io.on() function... with io.on('connection') I counted every each user logged in my app and put in an array, but I want to send different pages according to the socket.id that each user have.

How can I achieve this?

[...]
var row_of_users = [];//my array of users.
var visits = 0;//just a count of users logged in.

io.on('connection', function(socket){
    var user = socket.id;
    visits++;
    row_of_users.push(user);
    console.log(row_of_users);
[...]

Outside of the function I have my express function:

app.get('/', function(req, res) {
    if(row_of_users == 0) {
        res.sendFile(__dirname+'/index.html');
    } else if (row_of_users < 0) {
        res.sendFile(__dirname+'/wait-your-time.html');
}
Striezel
  • 3,274
  • 7
  • 19
  • 33
Daniel Vettorazi
  • 82
  • 1
  • 1
  • 7

1 Answers1

0

You need to store the socket io information with express middleware. See this answer: How to share sessions with Socket.IO 1.x and Express 4.x?

bc1105
  • 1,077
  • 6
  • 7