#Where to deploy with long-polling functions

6 messages · Page 1 of 1 (latest)

foggy kestrel
#

I'm a bit confused where Nitro hangs out in the deployment space and how these deployed environments manage setInterval alongside Nitro endpoints.

I have a function that runs every 10 seconds and is currently managed in Nitro like so:

export default defineEventHandler(async event => {
  try {
    let intervalId: NodeJS.Timeout
    const intervalTime = 10000 // 10 seconds in milliseconds

    intervalId = setInterval(() => {
      getLiveGameData(intervalId)
    }, intervalTime)
  } catch (e) {
    const error = createError({
      statusCode: 500,
      statusMessage: `Something went wrong with lolesports: ${e}`,
    })
    return sendError(event, error)
  }
})

Does this run for a few seconds every interval, or is the setInterval creating a long-running process that is never killed until the setInterval is killed using clearInterval(intervalId)?

I imagine the latter would get expensive quickly in a serverless environment. Or, am I overthinking this?

My understanding is serverless is ideal for short-running bits of code, not for long-running processes, and in fact, serverless providers tend to have a time-limit on long-running code. Is there a more ideal way to deploy a Nuxt application that utilizes Nitro extensively and doesn't depend on a serverless backend?

#

Sorry, just to clarify, this is within a Nuxt application and ideally, I can deploy it bundled together.

unique junco
# foggy kestrel I'm a bit confused where Nitro hangs out in the deployment space and how these d...

I don't think most "serverless" provider allow long-running lambdas/functions/... and rather use scheduled tasks/funcitons or similar

https://github.com/unjs/nitro/issues/1974 is the best to cover this IMO

GitHub

Nitro tasks allow on-off operations in runtime. Docs: https://nitro.unjs.io/guide/tasks The content you are editing has changed. Please copy your edits and refresh the page. Tasks Beta Give feedbac...

#

Another option would be a "poor mans cron" which is polling from another server or the user application and caching the results in the meantime

foggy kestrel
#

I'm not set on relying on a serverless provider, I've just honestly never deployed a Nuxt application outside of Netlify/Vercel and not familiar with alternative deployment approaches.