Reputation: 3356
What am I doing
I integrated opentok with our membership site and it is working perfectly fine. Our website is a member based site where you can signup and hold one on one video conferences with your clients who schedule a meeting with you.
What my understanding is of opentok
I understand that there is a monthly fee involved and then based on the number of minutes i utilized, i get billed. (of course after the first 10K minutes)
What I want to know
Since people will be signing up to use our service, I wanted to keep track of which user is consuming how many minutes. So is there a way to keep track of which user consumed how much talk time on OpenTok?
I will really appreciate any guidance here.
Upvotes: 2
Views: 655
Reputation: 2092
Piggybacking on @benjamintrent's comments, here are some OpenTok specific sample codes. It really depends on how you would want to bill your users, here are 2 cases:
If you want to start billing users the moment they go into the chat, you can send a post request to your server indicating start time:
session.on("sessionConnected", function(err){
if(!err){
$.post("/server/userId", {startTime: date.now()}, function(){...});
}
});
In most cases, you probably want to start billing when users are actually connected to the users they were supposed to be connected to:
session.on("streamCreated", function(stream){
if(stream.connection.connectionData == "username I was supposed to connect to"){
$.post("/server/userId", {startTime: date.now()}, function(){...});
}
});
You probably want to end a video chat session when he is disconnected from a session, or if the person you are supposed to talk to leaves the chatroom:
session.on("streamDestroyed", function(stream){
if(stream.connection.connectionData == "username I was supposed to connect to"){
$.post("/server/userId", {endTime: date.now()}, function(){...});
}
});
Unfortunately, if a user closes the browser, you will not get sessionDisconnected or streamDestroyed event. To overcome this, you can send a heartbeat post request to your server every 5 seconds. Your server will know that user has closed their browser if it stops receiving a heartbeat request and end the billing accordingly.
setInterval(function(){
$.post("/heartbeat/userId", {currentTime: date.now()}, function(){...});
}, 5000);
Hope that helped, good luck!
Upvotes: 1