Next.js Discord

Discord Forum

Best way to handle websockets.

Unanswered
Minskin posted this in #help-forum
Open in Discord
MinskinOP
I would like to use websockets, and i can get a connection easily. The problem is, i dont like to put my websocket hook into each client side component. What would be a good approach here? Maybe levering it into the layouts?

6 Replies

Asian black bear
Questions about code architecture require way more information and context. What have you tried, what are you achieving, why do you think something is not good/correct and how did you came to the conclusion etc.
Vaguely talking about some arbitrary websocket hooks (what even are those?) is pretty meaningless without any other information.
To keep it short, im using a laravel backend, and want to use a laravel echo client to access it. This is not a big problem, i can easily build a connection from my client component. The issue happens, when react rerender or similar, because im not able then to keep a single of the websocket.
MinskinOP
I now solved this by referencing a singleton through a context, which i then pass through my layout.tsx