Server-Sent Events a WebSockets
Dowiedzą się Państwo, jak Server-Sent Events zapewnia jednokierunkowe strumieniowanie w czasie rzeczywistym, jak wypada na tle WebSockets i long polling oraz kiedy wybrać każdą z tych technologii.
Server-Sent Events a WebSockets to bezpłatna lekcja WebSockets & Real-Time Systems with Spring na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej WebSockets & Real-Time Systems with Spring, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs WebSockets & Real-Time Systems with Spring zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Server-Sent Events a WebSockets” jest bezpłatna?
Tak — pełny tekst „Server-Sent Events a WebSockets” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu WebSockets & Real-Time Systems with Spring, przejdź na CoddyKit PRO. Kurs WebSockets & Real-Time Systems with Spring zawiera 4 lekcji w sumie.
Co nauczysz się w „Server-Sent Events a WebSockets”?
Dowiedzą się Państwo, jak Server-Sent Events zapewnia jednokierunkowe strumieniowanie w czasie rzeczywistym, jak wypada na tle WebSockets i long polling oraz kiedy wybrać każdą z tych technologii. Ćwiczysz WebSockets & Real-Time Systems with Spring z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć WebSockets & Real-Time Systems with Spring?
Nie wymagamy żadnego doświadczenia. WebSockets & Real-Time Systems with Spring w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Server-Sent Events a WebSockets”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji WebSockets & Real-Time Systems with Spring?
Tak. Każda lekcja WebSockets & Real-Time Systems with Spring zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Podstawy komunikacji w czasie rzeczywistym
- WebSockets a odpytywanie HTTP
- Podstawy protokołu WebSocket
- Server-Sent Events a WebSockets