Where to deploy with long-polling functions

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)
}
})
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?
6 Replies
Cody Bontecou
Cody Bontecou4mo ago
Sorry, just to clarify, this is within a Nuxt application and ideally, I can deploy it bundled together.
manniL
manniL4mo ago
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 API · Issue #1974 · unjs/nitro
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...
manniL
manniL4mo ago
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
Cody Bontecou
Cody Bontecou4mo ago
Thanks Alex. I started playing with that API (which is super nice btw), but ran into issues with dynamically managing when the task should be run: https://discord.com/channels/473401852243869706/1235506628544040981.
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Cody Bontecou
Cody Bontecou4mo ago
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.
manniL
manniL4mo ago
oh, I see. I'd raise a discussion or an issue in nitro for that actually!
Want results from more Discord servers?
Add your server