0

i'm trying to write a funny chatbot in node.js & socket.io. there is a deley of 5 second in bot reply. this is working good but when more then one user start chat with a bot there socket instances override to last one. take a look into the code

exports.newMessage = function(data, ack){
    var socket = this
    console.log(`newMessage listening`,data);
    botReply = looking4bot(data.message)

        setTimeout( ()=> {
            // if socket reconnected then find correct client
            if(client = users[data.to_user] )
            client.emit("newMessage", botReply)
        }, 5000)
}

i carefully read http://javascript.info/bind but did't help. i think bind is not useful here because whenever second user start chat before first's timeout, function rebind and intances override. anyone help me pls to slove issue

shivshankar
  • 2,067
  • 1
  • 18
  • 28
  • if (a = b) .... Mate, use == or === when you want to test equality! And a = this is considered as a very bad practice for a few years – Théophile Pace Sep 18 '17 at 06:54
  • if(client = users[data.to_user] ) i'm using assignment not equity also remove it and use socket.emit("newMessage", botReply) but same issue @ThéophilePace – shivshankar Sep 18 '17 at 06:58

0 Answers0