Websocket with Next appdir/RSC?
With the new RSC paradigm, how would we write a React app with Websocket (say for example a discord clone)? Do we still do it similar to how it has always been done or is there a new (better) pattern for these types of apps? How would this change if I already have a functional backend vs just starting everything from scratch?
Solution:Jump to solution
You generate the html with the server component and let the client component do the rest
6 Replies
Client component connecting to ws
Solution
You generate the html with the server component and let the client component do the rest
so as far as the websocket part itself is concerned, its the same as before, and the change that the layout stuff can be server components?
Yeah
got it, tysm!
If you don't want to use Websockets an alternative is HTTP streaming, user makes a GET request and connection is being kept open, the server can send new messages when it comes in via a POST request.
https://nextjs.org/docs/app/building-your-application/routing/router-handlers#streaming
Routing: Route Handlers
Create custom request handlers for a given route using the Web's Request and Response APIs.