top of page
This website was created by Code Enhancement Studio

[SHORT]how to solve Wix timeout after 14 seconds

This is a quick reply to a question that has been asked on Velo's discord.


When it comes to back-end functions timing out after 14s, it's important to note that the code still runs even though the connection is cut. If you do not return a result before the timeout, the promise will fail because the connection to the back end is gone. To work around this issue, there are two techniques you can use.


The first is to return a random (yet unique)`processId` to the user instead of your script result. When the script is done, save the result in your collection with the `processId`. On the front end, request the `Process` every 10 seconds to fetch the process result. Here is a basic example of the front-end code:

/**
* @author: code.enhancement.studio
* @description fetch a processId from the Process collection every `intervalTime = 10000` until it returns a value. Retry should stop after 10 iteration and return a timeout Error
* @param {string} processId - The Id of the desired process
* @param {number} [interval=10000] - The interval between requests in miliseconds
* @param {number} [maxRetry=10] - The maximum number of requests before a timeout error is thrown
* @returns {Promise<string>} A promise that resolves with the processId or throws a timeout error
*/

function fetchProcessId(processId, interval = 10000, maxRetry = 10) {  
    return new Promise((resolve, reject) => {
        let retry = 0;
        const intervalId = setInterval(() => {
            retry++;
            if (retry > maxRetry) {
                clearInterval(intervalId);
                reject(new Error('Timeout'));
               }
            const process = await wixData.get('Process', processId)
                .then(result => result.item[0])
                .catch(console.error);
            if (process) {
                 clearInterval(intervalId);
                 resolve(process);
             }
         }, interval);
    });
}

The second technique is similar but requires the real-time API. The back-end function still returns a `processId` but instead of querying the Process collection, use it to subscribe to a real-time channel. This way, the result is pushed to the user once done.


While the first option requires less setup, I recommend doing the second one because it will scale more easily and won't rely on wix-data API.


No matter which technique you choose, it's important to keep in mind that back-end functions are guaranteed to execute for up to a minute maximum. If your script takes longer than that, you will need to use a different approach.

63 views0 comments

Recent Posts

See All
Anchor 1

Need help with Velo ?

We provide tutoring and debugging session to all skills levels.

From amateurs to professional developers, help writing high-quality code for performance and security

Maybe later
bottom of page