Sam Bunting Sam Bunting - 1 month ago 5
Javascript Question

Socket.IO - Callback to user who emited only

I am making a chat application which requires users to log in, I have so far managed to get the login system working by using UserApp.io, but I cant seem to find a way which would send a "Callback" back to the user who has emited the information to the server.

So for index.html, when a login form is submitted, it would gather the values of the two fields and emit the data to the backend.

$('form#login').submit(function() {
var data = {};

data.email = $("#login_email").val();
data.password = $("#login_password").val();

socket.emit('user login', data);
});


In the index.js file, it receives the details and checks using the UserApp API that the user is valid and all the details are correct. It also retrieves information like the first and last name.

socket.on('user login', function (user) {
logger.info('Receiving login info for "' + user.email + '"...');

UserApp.User.login({"login": user.email, "password": user.password}, function (error, result) {
if (error) {
logger.error('Login failed: ' + error.message);
} else {
var userToken = result.token;
var userID = result.user_id;
console.log("User has logged in.");

UserApp.User.get({
"user_id": userID
}, function (error, result) {
if (error) {
logger.error(error.message);
} else {
logger.info(result[0]['first_name'] + " " + result[0]['last_name'] + " Has logged in!")
}
});
}
});
});


So here is my issue. I cant seem to find a way of giving a callback to index.html so it can show errors like "Incorrect username".

So is there a way of giving a callback to one person, more specificly, the person who submitted the login form?

Any help would be appreciated.

Thanks.

Answer

socket.io has acknowledgement callbacks, here are the docs

http://socket.io/docs/#sending-and-getting-data-(acknowledgements)

Add a callback function as the third argument when emitting

$('form#login').submit(function() {
    var data = {};

    data.email = $("#login_email").val();
    data.password = $("#login_password").val();

    socket.emit('user login', data, function (result) {
        console.log(result);
    });
});

and then the callback function server side can have an additional parameter which is the callback you defined when emitting

socket.on('user login', function (user, callback) {
    logger.info('Receiving login info for "' + user.email + '"...');

    UserApp.User.login({"login": user.email, "password": user.password}, function (error, result) {
        if (error) {
            logger.error('Login failed: ' + error.message);
        } else {
            var userToken = result.token;
            var userID = result.user_id;
            console.log("User has logged in.");

            UserApp.User.get({
                "user_id": userID
            }, function (error, result) {
                if (error) {
                    logger.error(error.message);
                } else {
                    logger.info(result[0]['first_name'] + " " + result[0]['last_name'] + " Has logged in!")
                    return callback('your results');
                }
            });
        }
    });
});