Continuous process while server is running
With NextJs my definition of a server is being challenged. I see a server as a program that can serve requests and do background tasks. With nextjs it seems to only do the former.
I have a web application where users can manage instances of a program on several machines. The machines run a kotlin program that manages the machine processes of the program instances locally. I want this kotlin program to communicate with the T3 server backend to listen for requests such as making a screenshot. I was thinking of doing this with a websocket.
When the user opens the /instances web page, they will see the running instances of the program on the machines. I want the user to be able to click on Screenshot button and then the backend notifies the kotlin programs that a screenshot needs to be made. The kotlin program makes the screenshot and then uploads it to the backend. Then the image can be served to the user.
If I put this in root.ts
this code runs each time a web page is visited or something. Instead I want this to be called when the T3 server starts and then keep running. Is that possible? Is that correct usage of NextJs? Should I take a whole different approach?
4 Replies
I just had some shower thoughts:
NextJS is supposed to be horizontally scalable. Therefore running an instance manager on the NextJS backend makes no sense.
I would have to make a third server application that the frontend can call for instance statusses and screenshots.
Correct me if I'm wrong
Solution
ChatGPT
Great answer
With realtime databases and subscribing to these events it should be able to resolve 🤔
Vercel Documentation
How to Add Realtime Communication on Vercel Applications
Learn how to integrate realtime communication to your Vercel application using providers like Ably or Supabase.