WebSocket on Server Component
Lets say I want to have a websocket listening for messages from a different API on the server, could I do something like this? Or would I have add the websocket connection to a client component and put all of this in a useEffect?
Solution:Jump to solution
Everything is effectively a lambda function, none of your Api is long living so your websockets will die very quickly
12 Replies
Yes, WebSockets are for clients only, you could use like pub/sub on server but the client has to listen to the server somehow
ahhh okay thanks
I had a feeling I would have to use a client component, but it would be cool to do something like this on the server
Websockets are not ideal for next at all
Solution
Everything is effectively a lambda function, none of your Api is long living so your websockets will die very quickly
yea I know, but I'm forced into using websockets because I need a constant data feed for certain parts of my teams application. dealing with realtime events and stuff
Then next/Vercel is the wrong stack
Or, if you still want to use next, you'll need to move your websocket onto a microservice that is long living / not next
even if it's on a client component?
No, client is fine
or something marked as
"use client"
Client can do whatever you need
I'm talking about websockets on the server
If you don't have any server websockets your fine
nope the websocket server is from a different microservice, I just thought it would be cool if we can revalidate the path like that, but it makes sense
Thank you for the help 🙂
I appreciate it
Yessir