Reputation: 51
I used PeerJS for my application [a voice and video chat application] and everything works great, connection to peer, video and voice call where working fine while development and testing was done on the same network until i hosted the application online. It stopped connecting to peers and keep reporting
PeerJS: iceConnectionState is disconnected, closing connections to [userid]
and
Error: Negotiation of connection to [userid] failed. at RTCPeerConnection.pc.oniceconnectionstatechange [as onicechange].
Any ideas on solving this issue?
Upvotes: 3
Views: 1575
Reputation: 2137
Found this answer and agree with Aminu. I found Twilio's TURN servers to be a good solution. I thought I'd give more detail in case it helped anyone.
I already have a Twilio account so that made it extra easy.
I made a PHP function to get me some Ice Servers. Like this...
define("TWILIO_ACC_ID","AC12345etc...");
define("TWILIO_AUTH_TOKEN","8675309");
require_once '/path/to/my/Twilio/autoload.php';
use Twilio\Rest\Client;
function twilio_iceServers() {
$twilio=new Client(TWILIO_ACC_ID,TWILIO_AUTH_TOKEN);
$token = $twilio->tokens->create();
return json_encode($token->iceServers);
}
Then for my javascript I load in my ice servers like this:
var twilio_iceServers=<?php print(twilio_iceServers()); ?>;
Then include those ice servers in my peerJS configs like this:
var peer_options={
key:'myKey',
host:'my.host.com',
port: 443,
secure: true,
config: {iceServers:twilio_iceServers}
};
Seems like these Ice Servers expire in about a day. Since with my app people could be leaving the window open for days at a time, I had to build a way to have those servers renewed.
//returns current time stamp in seconds
function now() {
return Math.floor(Date.now() / 1000);
}
//some variable that keeps track of whether I'm on a call or not
var call_status=null;
//save the current time stamp into the ice servers object on page load
twilio_iceServers.date=now();
//do a check every 60 seconds to see if the ice servers have expired. yes... not 12 hours
setInterval(function(){
//if ice server issued date is more than 12 hours ago AND we're not on a call. we renew the ice servers. If we are on a call, this will run about a minute after we get off the call. This is why the setInterval is set to be 1 minute instead of 12 hours.
if (twilio_iceServers.date<(now()-43200) && !call_status) {
$.post("/page/on/my/server/that/just/sends/back/twilio_iceServers",{},function(data) {
if (data) {
//save the new ice servers
twilio_iceServers=JSON.parse(data);
//save the new date of the server
twilio_iceServers.date=now();
}
});
}
},60000);
Is this all the REAL way to do all this? I DON'T KNOW! But it works for me. So there.
Upvotes: 0
Reputation: 51
I found a solution to the problem.
I had to buy ICE and TURN servers from Twilio to fix it. This gives me reliable connection and faster messaging.
You can try that.
Upvotes: 2