How do you implement real-time features in SvelteKit?
Answer
Real-time features in SvelteKit via multiple approaches: Server-Sent Events (SSE): server pushes events to clients over a persistent HTTP connection. Create a +server.js endpoint that returns a ReadableStream: return new Response(new ReadableStream({ start(controller) { const send = (data) => controller.enqueue(\`data: \${JSON.stringify(data)}\n\n\`); const interval = setInterval(() => send(getUpdate()), 1000); request.signal.addEventListener('abort', () => clearInterval(interval)); } }), { headers: { 'Content-Type': 'text/event-stream' } }). Client subscribes with new EventSource('/events'). WebSockets: SvelteKit doesn't have built-in WS support, but add them in the Vite plugin or via the adapter's hooks (e.g., adapter-node exposes the HTTP server). PartyKit: a platform purpose-built for SvelteKit real-time — add WebSocket rooms with a single import. Polling: simplest — use invalidate() to refresh load function data periodically.
Previous
What is SvelteKit's data loading waterfall problem and how do you solve it?
Next
What is the SvelteKit shallow routing feature?
More Svelte / SvelteKit Questions
View all →- Advanced How does Svelte 5's reactivity model with $state differ from Svelte 4?
- Advanced What is SvelteKit's data loading waterfall problem and how do you solve it?
- Advanced What is the SvelteKit shallow routing feature?
- Advanced How does SvelteKit handle static asset optimization?
- Advanced What is Svelte's approach to accessibility?