Next.js Discord

Discord Forum

Invoke fire and forget function Vercel

Unanswered
American Sable posted this in #help-forum
Open in Discord
Avatar
American SableOP
Im using Next.Js, and im trying to invoke a fire and forget function.
Basically I want that a user will send a request, to invoke the function and immediately return 200 to him (before waiting for the function to finish),
But when I'm using it in production, when I return 200 the function stops working.
Somthing like that:
if (!conversation?.verify || !conversation) {
          console.log("member is not verified yet");
          try {
            testingPreWhatsapp(conversation, teamId, message, "whatsapp");
            return new Response("message is not verified yet", {
              status: 200,
            });
          } catch (error) {
            console.log(error);
            return new Response("error", {
              status: 500,
            });
          }
        } else {
          try {
            TestingViaNonHttpRequestWhatsapp(message, teamId, conversation);
            return new Response("EVENT_RECEIVED", { status: 200 });
          } catch (error) {
            console.log(error);
            return new Response("error", {
              status: 500,
            });
          }
        }

5 Replies

Avatar
tafutada777
i’m a bit curious. what do you mean by immediate? do you involve the api synchronously from other languages like Python or Go?
Avatar
American SableOP
No, its all js in the same repo, basically I want to invoke the function and then return ok to the user while the function running in the background
Avatar
tafutada777
i guess you mean API. invoking api via http protocol from web browsers. JS is single thread event loop so no you can asynchronously invoke API from useEffect.
if you want to run background jobs in AWS lambda, you need pub sub like Kafka or Server Sent Event in Edge Runtime
Avatar
American SableOP
So for a small scale application I should use this option or create an express server and deploy on heroku or something like this?
Avatar
tafutada777
im not sure ur use case, but in general if you want long running job, AWS lambda is not suited as it last just 10sec. you want self-hosted or other managed services like GCP Cloud Run.