Not clear about some of the Asterisk SIP messages

am trying to implement a SIP UA and to do so, I studied the Asterisk console in debug mode for SIP. I tried to call one extension (A) from another extension (B).

The initial message flow up to the RINGING message, I understood, including the Digest Authentication part. These are:

(B) >—>INVITE-----[Asterisk]
Followed by a series of back-and-forth messages:

401 Unauthorized with NONCE
ACK
INVITE with correct Digest
TRYING
TRYING
RINGING
After the ringing phone (A) is picked up, I see the following exchange of message:

(A) >----> OK >-----> [Asterisk]

(A) <----< ACK>-----< [Asterisk]

                     [Asterisk] >----- OK ------> (B)

(A) >—(re)INVITE–> [Asterisk]

                     [Asterisk] <-----ACK-------< (B)

                     [Asterisk] >---(re)INVITE--> (B)

(A) >—TRYING -----> [Asterisk]

                     [Asterisk] <-----OK--------< (B)

(A) >-----OK--------> [Asterisk]

(A) <----ACK--------< [Asterisk]

I am writing the UA part on the (B) side and know the SDP for A beforehand and can generate the SDP for B, which is in my control. My call flow will always be from B to A. I can control all message going from (B). How can I reduce the above message flow? Also, I do not fully understand the need for so many messages after the initial SDPs are exchanged until RINGING. Or are they? Can someone mark a one-liner comment for the messages shown, specifically, what the OK and ACK correspond to?

Don’t implement a SIP UA by writing your own SIP stack. Don’t. Secondly implementing one by looking at the SIP debug from Asterisk probably won’t go well, if you are implementing one then you should be using the SIP specification and RFCs.

If you disable direct media the message flow above will be reduced. It’s trying to directly connect media. You will still have some OK/ACK for answering and acknowledge of OK reception.

@jcolp Thanks. I am not implementing a full SIP stack. Just basic SIP messages to make calls (nothing else, at least for now). I thought that was going to be an easy task. But it is not as easy as I thought. I could not come across any SIP Javascript UA that can run over plain UDP. They all need websockets. I am using some code from the sip module of Node. That said, your answer referring to disabling directmedia/canreinvite made me read on it here. The explanation is very useful. I am using the RFC and Asterisk console together as a reference.

@jcolp I want the ability for a user to enter a SIP Extension or a PSTN number in a browser to dial out from Asterisk. I use webRTC and then Kurento to get webRTC-to-RTP in my app server. Until this point, it works. It is this RTP stream that I want to connect to a SIP phone through Asterisk. I figured that I need a SIP UA agent in my app server to do this. If there is a better approach, please enlighten me. Just some pointers would suffice.

What you are doing and using is too specific to really give a recommendation…

@jcolp Your suggestion to disable directmedia was very helpful. In addition to stopping reINVITES, it helped me understand the cause of the SIP reInvite messages in the first place. Thanks!