0

I'm trying to learn about using node.js and socket.io. I was using before PHP and i have trouble with understanding how use callbacks in node.js.

I have part of code assigned to socket.io

if (validate_room(room)) {
    console.log('Room exist'); 
    io.sockets.clients(room).forEach(
        function (socket) {
            console.log(socket.id);
        });
    //do some fucnction using socket interface    
} else {
    console.log('Room not exist');
    //do other function using socket interface        
}

You can see, here that i need access to io.sockets object.

Function above validate_room

function validate_room(room) {

    mysql_connection.query('SELECT * FROM rooms WHERE room = ' + mysql_connection.escape(room), function(err, rows, fields) {
        if (err)
            throw err;

        if (rows.length.toString() > 0) {
            console.log('Validate room - true: ', rows.length.toString());

            return true;
        }
        console.log('Validate room - false: ', rows.length.toString());
        return false;
    });
}

I need second function to just return "true / false ".

When i was using "browser" i just put inside callback to another external function, but here i need access to socket.io object.

So i would like to have "if(validate_room(room))" here stop and wait for result true/false.

Maybe someone could point me, where i make mistake in my thinking.

Best regards Marc

Zoltan.Tamasi
  • 1,382
  • 13
  • 26
marc234
  • 13
  • 3

2 Answers2

0

Put something like this into your callback:

if (err)
    throw err;
afterRoomValidation(rows.length.toString() > 0);

and the function:

function afterRoomValidation(isValid) {
    if (isValid) {
        console.log('Room exist'); 
        io.sockets.clients(room).forEach(function (socket) { console.log(socket.id);  });
        //do some fucnction using socket interface    
    } else {
        console.log('Room not exist');
        //do other function using socket interface        
    }
}
Zoltan.Tamasi
  • 1,382
  • 13
  • 26
  • In node.js it's easy to get lost in the chain of callbacks. There are many ways and patterns and libraries to avoid this. Which one is best for you depends on the problem and the type of the app you develop. – Zoltan.Tamasi Mar 13 '13 at 20:48
0

function to just return "true / false ".

No. You can't do that just as you can't synchronously return the response from an AJAX call from a function?. Use callbacks:

function validate_room(room, validCallback, invalidCallback) {

    mysql_connection.query('SELECT * FROM rooms WHERE room = ' + mysql_connection.escape(room), function(err, rows, fields) {
        if (err)
            throw err;

        if (rows.length.toString() > 0) {
            console.log('Validate room - true: ', rows.length.toString());
            validCallback();
        } else
            console.log('Validate room - false: ', rows.length.toString());
            invalidCallback();
        }
    });
}

and

validate_room(room, function() {
    console.log('Room exist'); 
    io.sockets.clients(room).forEach(function (socket) {
        console.log(socket.id);
        //do some fucnction using socket interface
    });
}, function() {
    console.log('Room not exist');
    //do other function using socket interface        
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375