drachtio / drachtio-srf

drachtio signaling resource framework
https://drachtio.org
MIT License
169 stars 60 forks source link

Stopping call execution deep in a promise chain #39

Open WhiteyDude opened 5 years ago

WhiteyDude commented 5 years ago

Hi Dave,

This may be a dumb question as I'm not a node/modern JS guy, but how do you stop execution of a call on an error condition within chained promises?

https://gist.github.com/WhiteyDude/8dfe7c612cafa7dbf6f3970d5f8d946e

In the commented spot where I wish to stop execution, I've tried using a next() I've seen in examples, and I've tried returning a res.send(). However execution continues and the console.log is triggered. This works perfectly if I return it from within the main invite handler:

https://gist.github.com/WhiteyDude/98f21cc61e306ae340dcce7e691f67b1

I figure because it's within a promise (which I'm also new to), it doesn't return before execution continues. Is there a function I can call to do this, or a clean way to make this blocking?

Thanks!

davehorton commented 5 years ago

Hi there!

Since you aren't that familiar with Promises, I'd just suggest you go straight to using the "async / await " syntax for dealing with Promise-returning functions, since that is a bit easier.

using that, here is my version of your function, that does what you need:

https://gist.github.com/davehorton/c4d99329c43f8530c4e3f0ed95fe3189

A few points:

davehorton commented 5 years ago

I made a slight update to my example -- I'd forgotten to reject the incoming INVITE if we fail to connect to the media server in some way.

WhiteyDude commented 5 years ago

Wow, thanks so much Dave! I just wanted a little direction, you’ve completely refactored the code for the better!

I’ve removed my custom parsing function - I’m not amazing at regex either so it wasn’t great!

I can’t find in any of the docs or API reference where req.calledNumber or parseUri() are mentioned. Can you point me to these so I can see if I’m writing more duplicate functions?

Also thanks so much for for writing and releasing drachtio! It’s completely changing how I do voice, and it’s an absolutely pleasure. Looking forward to seeing your talk at CommCon next month!

WhiteyDude commented 5 years ago

Also just to confirm, in the scenario where the check_is_valid returns true, I'd put that further execution into the else?

https://gist.github.com/WhiteyDude/e1a9d19f09e098dd6d72865c301e440c#file-sample-js-L20

Thanks!

davehorton commented 5 years ago

yes, that looks good.

WhiteyDude commented 5 years ago

Thanks so much for your help!

davehorton commented 5 years ago

I forgot to point you to the docs you requested

req.calledNumber is described here:

https://drachtio.org/api#message-prop-called-number

Note that SipRequest inherits from SipMessage so it has access to all those properties

Srf.parseUri described here:

https://drachtio.org/api#Srf-properties

WhiteyDude commented 5 years ago

Thanks so much Dave!

I've come across one more thing I need to ask.

When I'm calling the media server's ep.bridge() I can await this, however this returns upon the bridge connecting. Is there a way to wait for the bridges disconnection (e.g. one party hanging up)? Or would this be best handled with the on('destroy') event? I've been await chaining to force sync operations for now, but I suppose I'll have to encapsulate this one within an if/then/else scenario if I need to catch it in the on('destroy').

Thanks!

Also, is there a better way to ask for help than github issues? I'm conscious people generally like to reserve this area for bug reports/feature requests etc.

davehorton commented 5 years ago

Sorry for the late response.

I think if you want to wait for one party hanging up, the dlg.on('destroy',.. is the way to do it. I usually have a handler for that and then explicitly destroy any related endpoints (you can also explicity call unbridge on an endpoint, but that is unnecessary if you are destroying either or both of them).

github issues are fine, and preferred in fact since hopefully other people may learn from them