N
Nuxt3mo ago
Mirage

How to access server context / global state from a websocket handler?

I have this simple websocket handler defined at server/routes/websocket.ts, I've already enabled the nitro.experimental.websocket flag in nuxt.config.ts and it works fine.
export default defineWebSocketHandler({
open(peer) {
console.log('peer connected')
},
message(peer, message) {
const text = message.text()

if (text === 'ping') {
peer.send('pong');
}
},
})
export default defineWebSocketHandler({
open(peer) {
console.log('peer connected')
},
message(peer, message) {
const text = message.text()

if (text === 'ping') {
peer.send('pong');
}
},
})
However, it doesn't seem there's a way to access a global state or server context like it's possible on event handlers:
export default defineEventHandler(event => {
// in event handlers context is accessible at `event.context`
})
export default defineEventHandler(event => {
// in event handlers context is accessible at `event.context`
})
I need this in order to initialize and access global services like database connections etc.
2 Replies
kapa.ai
kapa.ai3mo ago
Hi @Mirage. 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 explicitly document how to access the server context or global state (such as the event context or injected services) from within a Nitro WebSocket handler in Nuxt 3. In standard server API routes (using defineEventHandler), you have access to the event object, which provides context and allows you to access things like runtime config, cookies, and global services via event.context or similar patterns Forwarding Context & Headers. However, the available documentation and examples for WebSocket handlers (using defineWebSocketHandler) do not mention an equivalent way to access a global context or state object within the handler. If you need to share global services (like database connections) across your server routes and WebSocket handlers, a common approach in Nuxt is to create singleton instances in a separate module (for example, in a file under server/services/ or a composable), and import and use them directly wherever needed. This pattern is shown for other server-side use cases, such as CMS or database clients How to maintain singleton dependency injection in Nuxt 3, best practices to follow here?. To summarize:
- There is no documented way to access a request-specific context (like event.context) inside a WebSocket handler. - For global state or singleton services, define and export them from a module and import them in your WebSocket handler. If you need per-connection state, you may need to manage it manually within the handler logic. If you require more advanced integration, you may want to follow updates in the Nitro and Nuxt documentation or GitHub discussions, as WebSocket support is relatively new and evolving. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@1368579779309207656> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?