Issue with Sip.js Registration Failure Handling

671 views
Skip to first unread message

jazeb Sheraz

unread,
Jun 12, 2023, 5:58:13 AM6/12/23
to SIP.js
Dear Respected Community Members,

I hope this email finds you well. I am writing to seek your assistance regarding an issue I am facing with Sip.js version 0.21.2.

The problem arises when I attempt to register a user agent using incorrect username or password credentials. In such cases, I am unable to receive the expected "registrationFailed" event or any relevant status code. As handling registration failures is a fundamental feature for my application, I am in need of guidance to resolve this issue.

I have included the relevant code snippet below for your reference:
  const uri = UserAgent.makeURI("sip:al...@example.com");
  if (!uri) {
    throw new Error("Failed to create target URI.");
  }

  const options= {
    authorizationUsername: "username",
    authorizationPassword: "wrong-password",
    uri: uri
  };

  const userAgent = new UserAgent(options);

  userAgent.start()
    .then(() => {
      console.log("Connected");

      const registerer = new Registerer(userAgent);

      // Setup registerer state change handler
      registerer.stateChange.addListener((newState) => {
        switch (newState) {
          case RegistererState.Registered:
            console.log("Registered");
            break;
          case RegistererState.Unregistered:
            console.log("Unregistered");
            break;
          case RegistererState.Terminated:
            console.log("Terminated");
            break;
        }
      });

      // Send REGISTER
      registerer.register()
        .then((request) => {
          console.log("Successfully sent REGISTER");
          console.log("Sent request = " + request);
        })
        .catch((error) => {
          console.error("Failed to send REGISTER");
        });

    })
    .catch((error) => {
      console.error("Failed to connect");
    });


In console logs I get firstly 
          console.log("Successfully sent REGISTER");
          console.log("Sent request = " + request);
after that I get 
            console.log("Unregistered");
not able to get any reason for unregistered. 

Thank you very much for your attention and support. I look forward to hearing from you soon.




Phillip Munyororo

unread,
Jun 12, 2023, 2:41:49 PM6/12/23
to sip...@googlegroups.com
Hi,

Use event listeners: Instead of relying solely on the stateChange event, you can listen for additional events to capture more information about the registration failure. Sip.js provides various events that can be used for this purpose. For example, you can listen for the outgoingRequest event and inspect the response status code to determine the reason for the failure.

Regards 
Phillip Munyororo 

--
You received this message because you are subscribed to the Google Groups "SIP.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sip_js+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sip_js/2cc52636-0da7-4a1b-a2e6-98288471e9d3n%40googlegroups.com.

jazeb Sheraz

unread,
Jun 12, 2023, 3:00:31 PM6/12/23
to SIP.js
Hi  Phillip Munyororo. Thanks for your response. can you please write down a sample code for listening that  outgoingRequest event. it will be a great help for me. 

thanks again. 

Phillip Munyororo

unread,
Jun 13, 2023, 1:16:28 AM6/13/23
to sip...@googlegroups.com
Hi please see below:

const uri = UserAgent.makeURI("sip:al...@example.com");
if (!uri) {
  throw new Error("Failed to create target URI.");
}

const options = {
  authorizationUsername: "username",
  authorizationPassword: "wrong-password",
  uri: uri,
};

const userAgent = new UserAgent(options);

userAgent.start()
  .then(() => {
    console.log("Connected");

    const registerer = new Registerer(userAgent);

    // Setup registerer state change handler
    registerer.stateChange.addListener((newState) => {
      switch (newState) {
        case RegistererState.Registered:
          console.log("Registered");
          break;
        case RegistererState.Unregistered:
          console.log("Unregistered");
          break;
        case RegistererState.Terminated:
          console.log("Terminated");
          break;
      }
    });

    // Listen for outgoing requests
    userAgent.transport.on("outgoingRequest", (request) => {
      console.log("Outgoing Request:");
      console.log("Method: " + request.method);
      console.log("Request URI: " + request.ruri);
      console.log("Response status code: " + request.message.statusCode);
    });

    // Send REGISTER
    registerer.register()
      .then((request) => {
        console.log("Successfully sent REGISTER");
        console.log("Sent request = " + request);
      })
      .catch((error) => {
        console.error("Failed to send REGISTER");
      });
  })
  .catch((error) => {
    console.error("Failed to connect");
  });

I've added the
userAgent.transport.on("outgoingRequest", ...) event listener to capture outgoing requests. When the listener is triggered, it logs the method, request URI, and response status code to the console. You can access the statusCode property of the message object to retrieve the status code.
................................................................... 


Regards
Phillip Munyororo

jazeb Sheraz

unread,
Jun 13, 2023, 2:19:54 AM6/13/23
to SIP.js
Hi  Phillip Munyororo,


I have copied the same code you shared with me and in console it printed this error. Failed to connect TypeError: userAgent.transport.on is not a function.
because in sip.js 0.21.2 version it uses delegates like I gave in my below code. 



  const uri = UserAgent.makeURI("sip:al...@example.com");
  if (!uri) {
    throw new Error("Failed to create target URI.");
  }

  const options= {
    authorizationUsername: "username",
    authorizationPassword: "wrong-password",
    uri: uri,
     delegate: {
     onConnect: () => {},
     onDisconnect : () => {},
     onMessage : () => {}

please check this and let me know if I am doing something wrong here. and how can I get the events in 0.21.2 sip.js version .

thanks. 
sip_error.PNG

Phillip Munyororo

unread,
Jun 13, 2023, 2:39:19 AM6/13/23
to sip...@googlegroups.com
    // Delegate for outgoing requests
    userAgent.delegate = {
      onOutgoingRequest: (request) => {
        console.log("Outgoing Request:");
        console.log("Method: " + request.method);
        console.log("Request URI: " + request.ruri);
        console.log("Response status code: " + request.message.statusCode);
      },
    };

    // Send REGISTER
    registerer.register()
      .then((request) => {
        console.log("Successfully sent REGISTER");
        console.log("Sent request = " + request);
      })
      .catch((error) => {
        console.error("Failed to send REGISTER");
      });
  })
  .catch((error) => {
    console.error("Failed to connect");
  });

jazeb Sheraz

unread,
Jun 13, 2023, 3:05:37 AM6/13/23
to SIP.js
Tried with this as well but this event is not triggering and no console is printing. 

// Delegate for outgoing requests
    userAgent.delegate = {
      onOutgoingRequest: (request) => {
        console.log("Outgoing Request:");
        console.log("Method: " + request.method);
        console.log("Request URI: " + request.ruri);
        console.log("Response status code: " + request.message.statusCode);
      },
    };

Marcos Mariani

unread,
Jun 14, 2023, 1:55:39 PM6/14/23
to SIP.js
Hi jazeb,

You can use the requestDelegate available on the RegistererRegisterOptions

registerer.register({
requestDelegate: {
onAccept() {
resolve()
},
onReject(response) {
if (response.message.statusCode === 403) {
reject(response.message)
}
},
},
})

Reply all
Reply to author
Forward
0 new messages