Eventos enviados pelo servidor versus WebSockets
Aprenda como os Eventos Enviados pelo Servidor fornecem transmissão em tempo real em uma única direção, como se comparam a WebSockets e à consulta longa e quando escolher cada tecnologia.
Eventos enviados pelo servidor versus WebSockets é uma aula grátis de WebSockets & Real-Time Systems with Spring no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de WebSockets & Real-Time Systems with Spring, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de WebSockets & Real-Time Systems with Spring inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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.
Perguntas Frequentes
A aula “Eventos enviados pelo servidor versus WebSockets” é grátis?
Sim — o texto completo de “Eventos enviados pelo servidor versus WebSockets” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de WebSockets & Real-Time Systems with Spring, atualize para CoddyKit PRO. O curso de WebSockets & Real-Time Systems with Spring inclui 4 aulas no total.
O que vou aprender em “Eventos enviados pelo servidor versus WebSockets”?
Aprenda como os Eventos Enviados pelo Servidor fornecem transmissão em tempo real em uma única direção, como se comparam a WebSockets e à consulta longa e quando escolher cada tecnologia. Você pratica WebSockets & Real-Time Systems with Spring com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar WebSockets & Real-Time Systems with Spring?
Nenhuma experiência prévia é necessária. WebSockets & Real-Time Systems with Spring no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Eventos enviados pelo servidor versus WebSockets”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de WebSockets & Real-Time Systems with Spring?
Sim. Cada aula de WebSockets & Real-Time Systems with Spring inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Compreender a comunicação em tempo real
- WebSockets vs. sondagem HTTP
- Fundamentos do protocolo WebSocket
- Eventos enviados pelo servidor versus WebSockets