'Possible CORS error when looping between Twilio Functions and an external php endpoint

We're working on a looping alert system that sends an alert to our agent, then sends a request to our external PHP endpoint to sleep for an interval, then the endpoint calls our Twilio Function/Twilio Runtime Domain Endpoint again with an AJAX request to issue an alert, and loops.

Function A --> External Sleep Function (/Sleep) --> Function A

That's the flow that we need, and we can't get it to work. We can receive the request from Function A at /Sleep, but then can't get it back to Function A. We can also send request from the browser to /Sleep, then receive the request back at Function A, then receive at /Sleep but still can't get it back to Function A.

Basically, we can't get the loop to complete. Does anyone have experience making a system like this work?

Additional context: Our PHP endpoint (/Sleep) is sleeping for 3 seconds (3000 ms) for testing purposes. Sending a browser or curl request to /Sleep effectively fires a request that we receive at /Function-A, but once our /Function-A tries to send another request to /Sleep, the loop breaks. Here's our code at /Sleep:

    <script>
        function sleep(ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
        }
    
        async function twilioSleep() {
            await sleep( 3000 ); // 900000 = 15 min
            const Http = new XMLHttpRequest();
            const url='https://our-service.twil.io/alert';
            Http.open( 'GET', url );
            Http.send();
        }

        twilioSleep();
    </script>

Thank you so much.



Solution 1:[1]

Twilio developer evangelist here.

Twilio Functions have a timeout of 10 seconds and will not run beyond that. Also, once a Twilio Function either completes (you call the callback function) or is terminated, then any network connections it has open are also terminated.

So, if you are making the request from the Twilio Function to your /Sleep endpoint, but the /Sleep endpoint doesn't return a response until it has finished sleeping, then I would suggest that the Twilio Function is timing out, terminating the connection with your server and stopping the loop.

For this to work with Twilio Functions your PHP server will have to return a response to the Twilio Function immediately and perform the sleep asynchronously somehow. You could achieve this with a job scheduler or perhaps an asynchronous PHP library like spatie/async or coroutine. I'm not a PHP programmer, so I don't have a concrete recommendation, but hopefully these ideas help.

Edit

Ok, so based on the code in the question, the issue is that your PHP server is returning HTML and JavaScript to perform the sleep and make the next request. When you request that page with the browser, the browser interprets the JavaScript and makes the request. When the Twilio Function requests your /Sleep endpoint, it receives the HTML response and does not interpret it and run the JavaScript because it is not a browser. I do not know how that works with curl, but I suspect it really doesn't.

If you want your PHP server to sleep then make a new request, then you will need to implement that in PHP. To start with, the sleep function should help, and here's an article on ways to make HTTP requests in PHP.

But, as you work on this, remember that Twilio Functions will still have a 10 second timeout and if you sleep your PHP function longer than that then it will also get terminated. Unless you find a way to return a response to the Twilio Function and perform the sleep in the background, possibly using one of the async libraries I pointed to before, or a job scheduler.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1