Real-Time Features in Next.js: SSE, Polling, and WebSockets Without a Separate Server
Next.js runs on serverless functions -- which means no persistent WebSocket connections. But real-time features are non-negotiable for modern apps. Here are the patterns that actually work. Option ...

Source: DEV Community
Next.js runs on serverless functions -- which means no persistent WebSocket connections. But real-time features are non-negotiable for modern apps. Here are the patterns that actually work. Option 1: Server-Sent Events (SSE) SSE is HTTP-based, works with serverless, and handles one-way streaming (server to client). Perfect for notifications, live feeds, and AI streaming. // app/api/stream/route.ts export async function GET(request: Request) { const encoder = new TextEncoder() const stream = new ReadableStream({ async start(controller) { // Send initial connection message controller.enqueue(encoder.encode('data: connected\n\n')) // Stream updates const interval = setInterval(() => { const data = JSON.stringify({ time: Date.now(), value: Math.random() }) controller.enqueue(encoder.encode(`data: ${data}\n\n`)) }, 1000) // Cleanup on disconnect request.signal.addEventListener('abort', () => { clearInterval(interval) controller.close() }) } }) return new Response(stream, { headers: {