I’m seeking assistance to properly set up a Vercel server to handle my application’s logic.
My goal is to allow users to send a series of messages by pressing the enter button. These messages should be bundled together, and then sent to an AI API, which would process the bundle and return a response to the user in the chat.
Here’s how my logic works:
I’m using Node.js and Express to handle webhooks for incoming events.
When a message arrives, I store it in an array, timestamp it, and set a setTimeout() function for 5 seconds.
If another message arrives within that 5-second window, I clear the timeout, update the timestamp, and continue bundling messages within this custom time gap.
This setup works perfectly on my local machine. However, when I deploy it to Vercel, it behaves inconsistently. Sometimes it doesn’t work at all.
I would greatly appreciate any insights, suggestions, or solutions to resolve this issue on Vercel.
I was able to implement stream for the setTimout() function I received a gap in 5 seconds, but clearTimeout() is not working, and when I try to save some bundle of messages on each incoming event it rewrites and I receive the last message from 5 what I sent before (this could be resolved using KV storage to save session info, but it will rise a problem with high amount of requests)
After my huge investigation, I found out that I am trying to run a back-end application (Node.js) on Vercel, which it isn’t designed for. (Instead, maybe I need to use a VPS or a managed environment/app platform). In general, Vercel supports serverless functions, which I could use to run my back-end. These are essentially one-off functions that are run on a new server instance every time a request comes in, and the function restarts with the default state.
Could You confirm, that am I right and that I need to change hosting to achieve my app behavior
Serverless does work differently from VPS, as you discovered. Node projects can be deployed with serverless functions. Some project need to be adapted to get them working, and some might work more easily with a traditional server environment.
If the backend project you’re building makes more sense to you in a traditional server environment, then that’s probably the best choice. I know there are people who use Vercel for frontend deployment and host the backend somewhere else. That’s another option you could explore if you’re interested.