SIP.js icon indicating copy to clipboard operation
SIP.js copied to clipboard

Sip.js Reconnection Issue when Web socket connection Lost or Network connectin lost

Open m-yunus opened this issue 1 year ago • 5 comments

I am facing an issue with reconnection handling in SIP.js when the WebSocket connection is lost or the network is disconnected. After a disconnection, the onDisconnect delegate is triggered, but the UserAgent doesn't reconnect properly, and I can't maintain registration automatically. const transportOptions = { server: "wss:///ws", traceSip: true, iceCheckingTimeout: 35000, register: true, stunServers: ["stun:stun.l.google.com:19302"], turnServers: [ { urls: "t", username: "", credential: "", }, ], connectionTimeout: 5, maxReconnectionAttempts : 100, reconnectionTimeout: 4, keepAliveInterval: 10 }; const handleRegister = (event) => { const uri = UserAgent.makeURI(sip:${sipUri}@); const userAgentOptions = { authorizationUsername: sipUri, authorizationPassword: "", transportOptions, uri, delegate: { onInvite, onDisconnect: (error) => { alert("disconnected") console.log("Network connectivity lost"); if (!error) { console.log("User agent stopped"); } }, onConnect: () => { alert("connected") handleRegister(); console.log("Network connectivity established"); },

}

};

const ua = new UserAgent(userAgentOptions); setUserAgent(ua);

ua.start() .then(() => { const registerer = new Registerer(ua); return registerer.register({ requestDelegate: { onAccept(response) { isRegistering = false; alert("registerred successfully") console.log("UserAgent registered successfully", response); }, onReject(sip) { isRegistering = false; // Reset flag on failure console.log("UserAgent registration failed", sip);

    },
  },
});

}) .catch((error) => { isRegistering = false; console.error("Error starting the UserAgent:", error); }); } I’ve reviewed the SIP.js documentation and attempted to use the onDisconnect and onConnect delegates for handling reconnections, but I'm unable to achieve reliable re-registration.

Any guidance or recommendations would be greatly appreciated.

m-yunus avatar Dec 09 '24 09:12 m-yunus

Is there any solution on this, How to handle the same ?

greatapps avatar Dec 14 '24 16:12 greatapps

You have to call reconnection method inside onDisconnect delegate. Like,

const reconnectionAttempts = 3;
const reconnectionDelay = 4;
let attemptingReconnection = false;
let shouldBeConnected = true;

userAgent.delegate.onDisconnect = (error) => {
  if (error) {
    attemptReconnection();
  }
};

const attemptReconnection = (reconnectionAttempt = 1)=> {
  // If not intentionally connected, don't reconnect.
  if (!shouldBeConnected) {
    return;
  }

  // Reconnection attempt already in progress
  if (attemptingReconnection) {
    return;
  }

  // Reconnection maximum attempts reached
  if (reconnectionAttempt > reconnectionAttempts) {
    return;
  }

  // We're attempting a reconnection
  attemptingReconnection = true;

  setTimeout(() => {
    // If not intentionally connected, don't reconnect.
    if (!shouldBeConnected) {
      attemptingReconnection = false;
      return;
    }
    // Attempt reconnect
    userAgent.reconnect()
      .then(() => {
        // Reconnect attempt succeeded
        attemptingReconnection = false;
      })
      .catch((error) => {
        // Reconnect attempt failed
        attemptingReconnection = false;
        attemptReconnection(++reconnectionAttempt);
      });
  }, reconnectionAttempt === 1 ? 0 : reconnectionDelay * 1000);
};

shibran avatar Jan 30 '25 13:01 shibran

i dont hear anything on when an outgoing call not hearting early media any issue on this

m-yunus avatar Feb 05 '25 05:02 m-yunus

After network reconnection (e.g., switching from Wi-Fi to mobile data), the WebSocket and SIP session reconnect successfully. However, even though the call continues and ICE renegotiation completes without errors, audio is not heard on either side post-reconnection.

jayparmarmagic avatar Jul 16 '25 12:07 jayparmarmagic

I have the same issue - It seems that the local web RTC connection remains in place when the connection is lost and then recovered. There should be a renegotiation or some kind of web RTC rebuild to hook up the session after the reinvite.

MadEddieFFS avatar Jul 23 '25 15:07 MadEddieFFS