0

I've learned that firebase puts it's API functions into sleep mode, which is a problem for me. I call a function called createCheckoutSession every-time a user enters their info on the register, and then it'll initiate the API function in firebase. If this doesn't get initiated every 2-3 minutes, it will shut down, and take 8 seconds to load up. That is not very good.

So how do I call createCheckoutSession when a user lands on the regsiter page, before they type in all there details, so that by the time they hit next, the function in fireabase will already be up and running. Basically to initiate the API function in firebase, without actually rendering the stripe page, so it can be kept running in the bkg.

here's the stripe function:

    export async function createCheckoutSession(activtyStatus){

 
  let user = firebase.auth().currentUser;

  //price ID with trial TEST ID

  //price ID with trial LIVE ID
  var price = 'price_1IGW85KDPaWWeL1yjWsi9oRa'

  if (activtyStatus == "canceled") {
    //live price
    price = 'price_1IfmDsKDPaWWeL1ywjMTGarh'
    
    
  }
  

      const checkoutSessionRef = await firestore
      .collection('customers')
      .doc(user.uid)
      .collection('checkout_sessions')
      .add({
        price: price,
         success_url: "http://localhost:3000/clients",
         cancel_url: "http://localhost:3000/signin",

    });
    
      // Wait for the CheckoutSession to get attached by the extension
            checkoutSessionRef.onSnapshot(function (snap) {
              const { error, sessionId } = snap.data();
              if (error) {
            // Show an error to your customer and 
            // inspect your Cloud Function logs in the Firebase console.
              console.log(`An error occured: ${error.message}`);
            }
            if (sessionId) {
            // We have a session, let's redirect to Checkout
            // Init Stripe

           

            
            
            const stripe = window.Stripe('pk_live');
            
           console.log("line 116 checkout.js")
            stripe.redirectToCheckout({sessionId})
            console.log("logged stripe")
            
          }
      });
    }
  • *firebaser here* "firebase puts it's API functions into sleep mode" Can you clarify what you mean by that, or provide a link to the relevant documentation? If you mean Cloud Functions' cold starts, then I recommend looking at what others have tried already: https://www.google.com/search?q=how+to+prevent+cloud+functions+cold+start, including https://stackoverflow.com/q/51782742 and more from https://www.google.com/search?q=how+to+prevent+cloud+functions+cold+start+site:stackoverflow.com – Frank van Puffelen Apr 25 '21 at 01:16
  • Thanks for replying @FrankvanPuffelen , unfortunately those won't work as I am calling Stripe, which automatically redirects to page, so i'm not sure a way around that –  Apr 25 '21 at 01:41
  • "firebase puts it's API functions into sleep mode" Can you clarify what you mean by that? – Frank van Puffelen Apr 25 '21 at 01:50
  • @FrankvanPuffelen yes, so basically I read online, I will try to find document, but it basically says, if you are using an extension, such as the one I am using, `Run Subscription Payments With Stripe`, then they contain functions. If the are not invoked every 2-3 minutes, they are put into `sleep mode`, where they basically shut down, and they take time to load back up again -- cold starting. It can be upwards of 8 seconds. –  Apr 25 '21 at 03:01
  • That is called a called start, and is what the links I provided in my first comment describe. There is no certain workaround for them at the moment. – Frank van Puffelen Apr 25 '21 at 03:39

0 Answers0