N
Nuxt2mo ago
Tio

Publish a message on websocket from another endpoint

Hello, I am using NuxtHub and enabled experimental websockets from Nitro and workers from NuxtHub. I have an endpoint server/api/ws.ts:
export default defineWebSocketHandler({
open(peer) {
// We subscribe to the 'visitors' channel
peer.subscribe("visitors");
// We publish the number of connected users to the 'visitors' channel
peer.publish("visitors", peer.peers.size);
// We send the number of connected users to the client
peer.send(peer.peers.size);
},
close(peer) {
peer.unsubscribe("visitors");
// Wait 500ms before sending the updated locations to the server
setTimeout(() => {
peer.publish("visitors", peer.peers.size);
}, 500);
},
});
export default defineWebSocketHandler({
open(peer) {
// We subscribe to the 'visitors' channel
peer.subscribe("visitors");
// We publish the number of connected users to the 'visitors' channel
peer.publish("visitors", peer.peers.size);
// We send the number of connected users to the client
peer.send(peer.peers.size);
},
close(peer) {
peer.unsubscribe("visitors");
// Wait 500ms before sending the updated locations to the server
setTimeout(() => {
peer.publish("visitors", peer.peers.size);
}, 500);
},
});
I would like to publish a message from another endpoint server/api/test.ts
export default defineEventHandler(async (event) => {
// How to publish a message in the the visitors websocket from an EventHandler?
});
export default defineEventHandler(async (event) => {
// How to publish a message in the the visitors websocket from an EventHandler?
});
How can I do that? Or should everything be handled in the websocket endpoint directly? Thanks for the help.
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @Tio. 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 provided do not contain explicit information on how to publish a message to a WebSocket channel from a separate HTTP endpoint (such as server/api/test.ts) when using Nitro's experimental WebSocket support in Nuxt or NuxtHub. The available documentation and examples show how to handle WebSocket connections and events within the WebSocket handler itself using defineWebSocketHandler, but they do not describe a built-in or documented way to access or interact with WebSocket peers or channels from outside the WebSocket handler context (such as from a standard API route) Nuxt 3: Integrating WebSockets in Nuxt and Nitro. If you need this functionality, you may need to look for additional Nitro or NuxtHub documentation, or consider architectural alternatives such as using a shared storage or pub/sub mechanism (like Redis) to communicate between endpoints and WebSocket handlers. However, this approach is not covered in the provided sources. If you have access to the latest Nitro or NuxtHub documentation, it may be worth checking there for any new APIs or patterns that support this use case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@932053571342573599> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?