Skip to main content
InterviewHubPRO
Section Overview
Q33

Question 33 of 50

Part 3: Advanced Internals, Performance & Production (Senior / Staff)

66%
Nuxt & Universal SSR (50 Qs)•Part 3: Advanced Internals, Performance & Production (Senior / Staff)

How do you implement WebSockets in Nuxt 3 with CrossWS?

senior LevelCode ExampleFormsPerformanceVue & Nuxt

In Simple Words: Nuxt 3 supports native WebSocket handling via Nitro's integration with CrossWS (Universal WebSocket layer for Node, Deno, Bun, and Cloudflare):

typescript
// server/routes/_ws.ts
export default defineWebSocketHandler({
  open(peer) {
    peer.subscribe('chat-room');
    peer.publish('chat-room', `${peer} joined the chat`);
  },
  message(peer, message) {
    peer.publish('chat-room', message.text());
  },
  close(peer) {
    peer.publish('chat-room', `${peer} left`);
  }
});