Reputation: 2474
I am working on live chat for admin and customer (nodejs+socketio+express). In between chat when customer waiting more than two minutes for admin response I need to show message to customer side "please try sometime later"
for example client getting response from this event
socket.on('Admin Response',function(msg){
});
How to track this event not get trigger from the last two minutes?
Upvotes: 1
Views: 65
Reputation: 2916
A simple way to do this is as follows :
var timeout=setTimeout(sendMessage,120000);
socket.on('Admin Response',function(msg){
clearTimeout(timeout);
var timeout=setTimeout(sendMessage,120000);
});
For a larger application, I would store last send time (and other information) in an Object that defines the connection. Have a regular process looks for connections with no activity, and handle appropriately.
Upvotes: 1
Reputation: 497
You can wrap the the socket event into an observable, then just use the .debounce operator to only trigger the "please try sometime later" text after 2 minutes of inactivity (untested):
var adminResponses = Rx.Observable.fromEventPattern(
function add (h) {
socket.on('Admin Response',h);
}
).debounce(120000); //120000 ms = 2 mins
var subscription = adminResponses.subscribe(
function(msg) {
/* show "please try sometime later" text */
});
Upvotes: 1