Server-Sent EventsとWebSockets
Server-Sent Eventsが一方向のリアルタイムストリーミングを提供する仕組み、WebSocketsやロングポーリングとの違い、それぞれを選ぶべき場面を学びます。
「Server-Sent EventsとWebSockets」はCoddyKit上の無料WebSockets & Real-Time Systems with Springレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWebSockets & Real-Time Systems with Spring学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 WebSockets & Real-Time Systems with Springコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
AI チューターと学ぶ WebSockets & Real-Time Systems with Spring — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「Server-Sent EventsとWebSockets」レッスンは無料ですか?
はい。「Server-Sent EventsとWebSockets」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebSockets & Real-Time Systems with Springコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebSockets & Real-Time Systems with Springコースには全4レッスンが含まれています。
「Server-Sent EventsとWebSockets」で何を学びますか?
Server-Sent Eventsが一方向のリアルタイムストリーミングを提供する仕組み、WebSocketsやロングポーリングとの違い、それぞれを選ぶべき場面を学びます。 ブラウザで直接実行するハンズオンコードでWebSockets & Real-Time Systems with Springを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
WebSockets & Real-Time Systems with Springを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのWebSockets & Real-Time Systems with Springは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Server-Sent EventsとWebSockets」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このWebSockets & Real-Time Systems with Springレッスンでコードを書いて実行できますか?
はい。すべてのWebSockets & Real-Time Systems with Springレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- リアルタイム通信を理解する
- WebSocketsとHTTPポーリングの比較
- WebSocketプロトコルの基礎
- Server-Sent EventsとWebSockets