Connecting to websockets in app router
I am looking to build an application that does the following:
- connect to zoom
- get users from zoom
- subscribe to their status
I can’t find anything on websockets for nextjs app router.
Does anyone know how I would do this?
Would prefer to subscribe on the server best solution i got is to wrap the layout in a client component and the client component gets the users and subscribes to the status however this would be slow
3 Replies
Doing this highly depends on you deployment strategy. (If you want to subscribe on the server).
- If you deploy to vercel (or any lambda-like service), you will need to how a spearate application on something long running (container, VPS) that is going to preserve the connection. Your nextjs app will call this service and get the data as it needs. This is probably the best solution.
- If you are already deploying to your own server or a container, you can use the instrumentation hook in nextjs. It will run only once on server startup. You can create a subscription there and store it in a global variable to interact with it. This is also possible on serverless, but I would highly advise against it, because you will pay the connection cost on every cold start. (assuming you cannot respond to a request untinl the WS has been established.
Yeh Cool thank you for your help
Agree with Hackder. Persist connections like sockets etc cant be achieved using server lambda functions as they just start when a request comes via an event listener type interface. You will need a seperate dedicated server on a linux instance or something for that which runs all the time for which you will have to give a fixed amount of money per month instead of the usual pay per usage scheme of lambda functions.