Twilio TURN token.iceServers not working when using different phone network

Scenario 1: both phones using the same network (1 phone set as WIFI hotspot and the other connect to that WIFI hotspot). Result: working properly.
Scenario 2: each phones using their own 4G networks from different provider. Result: not working, the pc.connectionState always end up "failed".

PHP:
$twilio = new Client($sid, $token);
$token = $twilio->tokens->create(["ttl" => 3600]);
echo $token->iceServers;

Javascript:
var pc = new RTCPeerConnection({"iceServers":JSON.parse(data)});
//data is from the PHP above

Tagged:

Best Answer

  • yaohan
    yaohan
    Accepted Answer

    Thanks for the reply.

    1. You are right, I mistakenly omit the json_encode when asking question.
    2. I wrote bare minimum WebRTC app (JS + PHP) to test the TURN server from Twilio, turns out it working well even when using two different carriers.

    So, this problem solved. The real problem is at my real app, not at Twilio's TURN.

Answers

  • I'm not a PHP developer, but I think you need to encode the $token->iceServers as JSON if you want to fetch and parse it as JSON in the front-end. Try:

    $twilio = new Client($sid, $token);
    $token = $twilio->tokens->create(["ttl" => 3600]);
    echo json_encode($token->iceServers);
    

    Note that when devices are on the same network they can share their local network IP address and port number in order to connect, so would not need to use the STUN/TURN servers.