0

I'm hosting an expressjs app on Vercel, at some point it uses the spreadsheet api to store a generated product key. The function looks like this:

async function addActivationCode(activationCode)
{
    await googleSheets.spreadsheets.values.append({
        auth: googleAuth,
        spreadsheetId: sheetID,
        range: "ActivationCodes!A:C",
        valueInputOption: "USER_ENTERED",
        resource: {
            values: [
                [activationCode, "1", "0"]
            ]
        }
    })
}

This function works perfectly fine when hosted locally. But in Vercel's Realtime logs I get a FetchError saying: Client network socket disconnected before secure TLS connection was established.

the try-catch block that calls this function is the following:

try
{
    await addActivationCode(generateActivationCode())
}
catch(err)
{
    console.log(err)
    console.log("Failed to add new activation code.")
    return
}

Note that it is not the only function that sends an append request to the spreadsheet api. There's another function used to add users and it's working fine. Here it is:

async function addUser(username, password)
{
    // Generate salt and hash the password in one command
    const hashedPassword = await bcrypt.hash(password, 10)

    // Add user to the Users sheet
    await googleSheets.spreadsheets.values.append({
        auth: googleAuth,
        spreadsheetId: sheetID,
        range: "Users!A2:B",
        valueInputOption: "USER_ENTERED",
        resource: {
            values: [
                [username, hashedPassword]
            ]
        }
    })
}

I found another post talking about this issue but not in Vercel. It was related to proxies. My knowledge about proxies atm is limited so I didn't quite understand how to fix this in Vercel.

Any help is appreciated.

Dtomper
  • 43
  • 6

1 Answers1

1

I found where the problem is coming from. It's Vercel's limits. For users using the free plan, their app can't take more than 10 seconds to respond to a request. For instance, the function I have in my app that's responsible for registering users does 5 things:

  1. It checks if the username already exists by retrieving the list of users from google spreadsheet.
  2. It checks if the given product key is valid
  3. It adds the user
  4. It deletes the product key used during registration
  5. It generates a new product key and appends it to the table of keys.

That's 5 god damn requests sent to the spreadsheet api! That definitely takes more time than the limit to complete.

Dtomper
  • 43
  • 6