Server-Sent Events frente a WebSockets
Aprenda cómo Server-Sent Events proporciona streaming en tiempo real unidireccional, cómo se compara con WebSockets y long polling, y cuándo elegir cada tecnología.
Server-Sent Events frente a WebSockets es una lección gratuita de WebSockets & Real-Time Systems with Spring en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de WebSockets & Real-Time Systems with Spring, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de WebSockets & Real-Time Systems with Spring incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Another Real-Time Option
WebSockets are not the only option. Server-Sent Events (SSE) give a simpler, one-way streaming channel from server to client over plain HTTP.
What Are Server-Sent Events?
SSE pushes a continuous stream of text events to the browser over one long-lived HTTP connection, received via the EventSource API. It is one-way only.
The SSE Wire Format
SSE uses a simple, line-based wire format over the text/event-stream content type — just data: and optional event: fields.
HTTP/1.1 200 OK
Content-Type: text/event-stream
data: First update
event: price
data: {"symbol":"ACME","price":42}
Receiving Events in the Browser
The client subscribes with EventSource, and the browser automatically reconnects if the stream drops — no manual retry logic needed.
const source = new EventSource('/stream');
source.onmessage = function (e) {
console.log('message:', e.data);
};
source.addEventListener('price', function (e) {
console.log('price event:', e.data);
});Built-In Reconnection
SSE has reconnection built in: the server sets the delay with a retry: field, and the client sends Last-Event-ID to resume where it left off.
SSE vs WebSockets
SSE vs WebSockets: SSE is one-way over plain HTTP with auto-reconnect and text only; WebSockets are full-duplex, use their own protocol, and support binary.
SSE vs Long Polling
Versus long polling, SSE keeps one connection open and streams many events, avoiding the cost of reopening a connection for every message.
When to Use SSE
Reach for SSE for one-way feeds: live notifications, activity streams, stock tickers, dashboards, job progress, and live scores.
When to Use WebSockets
Choose WebSockets when the client sends frequently too: chat, multiplayer, collaborative editing. If you only need server-to-client, SSE is simpler.
Limitations of SSE
SSE has limits: text only, no binary; browsers cap connections per domain (eased by HTTP/2); and older environments may lack support.
A Decision Helper
A simple rule of thumb decides most cases — need client-to-server or binary? Pick WebSocket. Otherwise, SSE.
def choose_transport(needs_client_to_server, needs_binary):
if needs_client_to_server or needs_binary:
return 'WebSocket'
return 'Server-Sent Events'
print(choose_transport(False, False)) # notifications
print(choose_transport(True, False)) # chatQuick Check
One feed, one direction, one connection — how well do you know SSE?
Recap
You learned Server-Sent Events: simple one-way streaming over HTTP with auto-reconnect, how they compare to WebSockets and long polling, and when to use each.
Preguntas frecuentes
¿La lección «Server-Sent Events frente a WebSockets» es gratis?
Sí — el texto completo de «Server-Sent Events frente a WebSockets» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de WebSockets & Real-Time Systems with Spring, actualiza a CoddyKit PRO. El curso de WebSockets & Real-Time Systems with Spring incluye 4 lecciones en total.
¿Qué aprenderé en «Server-Sent Events frente a WebSockets»?
Aprenda cómo Server-Sent Events proporciona streaming en tiempo real unidireccional, cómo se compara con WebSockets y long polling, y cuándo elegir cada tecnología. Practicas WebSockets & Real-Time Systems with Spring con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar WebSockets & Real-Time Systems with Spring?
No se requiere experiencia previa. WebSockets & Real-Time Systems with Spring en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Server-Sent Events frente a WebSockets»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de WebSockets & Real-Time Systems with Spring?
Sí. Cada lección de WebSockets & Real-Time Systems with Spring incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Comprensión de la comunicación en tiempo real
- WebSockets frente a sondeo HTTP
- Fundamentos del protocolo WebSocket
- Server-Sent Events frente a WebSockets