What's the difference between WebSocket and Server-Sent Events (SSE)?
// WebSocket example
const ws = new WebSocket('ws://example.com');
ws.send('Client message');
// SSE example
const sse = new EventSource('http://example.com/events');
sse.onmessage = (event) => {
console.log('Server message:', event.data);
};
Key differences between WebSocket and SSE include: 1) SSE is unidirectional (server-to-client only), while WebSocket is bidirectional, 2) SSE works over standard HTTP, while WebSocket uses its own protocol, 3) SSE has automatic reconnection built-in, while WebSocket requires manual implementation, 4) SSE supports event types and filtering, 5) SSE is text-only, while WebSocket supports both text and binary data. Choose based on your needs: SSE for server push notifications, WebSocket for real-time bidirectional communication.