socket.io emit to specific user and give them x amount of time to respond
<p>I have an online chat. It uses rooms. If a user sends message and the other user is not online, it should increment the "missed messages" counter. I tried to create a timeout with <code>setTimeout</code> and if they emit an event it clears that timeout.</p>

<p>However <code>chatOnline</code> doesn't fire as I expected to, which leads to it always reporting the user is offline and incrementing the counter for <code>missed_texts</code> column in <code>rethinkdb</code> (which is not shown because it isn't relevant).</p>

<p>How can I retrieve if the user is online from socket.io? My goal is to avoid having to store presence info in the database, which could get out of control quickly.</p>

<p>Code I tried:</p>

<pre><code>socket.on('chatSend',function(data){
    if(socket.client.user.room_id !== null){

        //were storing some crap in the socket object for easy retrieval. 
        data.user_id = socket.client.user.id;
        data.room_id = socket.client.user.room_id;
        data.timestamp = ~~(new Date() / 1000);

        //insert message into chat table
        r.table('chat').insert(data).run().then(function(res){


            //retrive generated record from table
            r.table('chat').get(res.generated_keys[0]).run().then(function(data2){


                io.sockets.in(data2.room_id).emit('chatNew',data);//emit to all users in room

                log('chatSend');

                //attempt to see if the other user is online
                getOtherUser(data2.room_id,socket.client.user.id,function(tid){

                    log('other user id: %d',tid);

                    //all users automatically join a room matching their user id when connecting.
                    //unsure how to see if the user is online. this doesnt work.
                    //this is what i need help with. retrieving the other users socket resource if they are online,
                    //and if they are not then return null or false, etc so i can work with that.
                    var othersocket = io.sockets.in(tid);


                    //if timeout completes before they respond, they are not online. 
                    var tmptime = setTimeout(function(){
                        log('other user not online.');
                        othersocket.removeListener('chatOnline',tmpfunc);
                        missedTexts(data2.room_id,tid,'INCR');
                    },5000);

                    var tmpfunc = function(){
                        clearTimeout(tmptime);
                        //remove the listener
                        othersocket.removeListener('chatOnline',tmpfunc);
                    };

                    //emit chatOnline to other user socket
                    //when they respond, cleartimeout, resulting in counter not being incremented.
                    othersocket.on('chatOnline',tmpfunc);
                    othersocket.emit('chatOnline');


                });

            });
        }); 
    }
});
</code></pre>