N
Nuxt5mo ago
TorNato

Nitro Websocket Cookies

Is there a better way to get the cookie from the open method than peer.request.headers.get()? This seems janky when I can normally just getCookie(event, ...), and parsing cookie strings is an annoyance
4 Replies
kapa.ai
kapa.ai5mo ago
Hi @TorNato. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry, but the knowledge sources do not contain explicit information about handling cookies in Nitro WebSocket handlers, or whether there is a more ergonomic alternative to using peer.request.headers.get() for accessing cookies (such as a getCookie utility that works with WebSocket events). Most of the documented cookie utilities like getCookie and useCookie are designed for standard HTTP event handlers in Nuxt/Nitro, where you have access to the H3 event object. There is no mention in the provided sources of a built-in helper for extracting cookies directly from a WebSocket peer or event in Nitro. The typical approach for HTTP handlers is to use getCookie(event, ...), but this does not appear to be documented for WebSocket connections. Therefore, unless there is an undocumented feature, parsing the cookie string from peer.request.headers (as you are currently doing) seems to be the necessary approach for now. If you want to avoid manual parsing, you could use a cookie parsing utility, but this is not covered in the official documentation or examples provided in the sources above. If you are looking for official guidance or a more streamlined API, I recommend following the Nitro and Nuxt repositories for updates or opening a feature request if this is a common use case for your application. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@710736486705791026> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
TorNato
TorNatoOP5mo ago
lol what that's not very helpful
TorNato
TorNatoOP5mo ago
also the docs (https://nitro.build/guide/websocket) seem very sparse (read: nonexistent) on what a "topic" is, as in peer.subscribe(topic)
WebSocket - Nitro
Nitro natively supports a cross platform WebSocket API

Did you find this page helpful?