0Pricing
WebSockets & Real-Time Systems with Spring · บทเรียน

เหตุการณ์ที่ส่งจากเซิร์ฟเวอร์เทียบกับ WebSockets

เรียนรู้ว่าเหตุการณ์ที่ส่งจากเซิร์ฟเวอร์ให้การสตรีมแบบเรียลไทม์ทางเดียวอย่างไร เปรียบเทียบกับ WebSockets และการสำรวจแบบยาวอย่างไร และควรเลือกเทคโนโลยีใดในสถานการณ์ใด

เหตุการณ์ที่ส่งจากเซิร์ฟเวอร์เทียบกับ WebSockets เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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))   # chat

Quick 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.

คำถามที่พบบ่อย

บทเรียน “เหตุการณ์ที่ส่งจากเซิร์ฟเวอร์เทียบกับ WebSockets” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เหตุการณ์ที่ส่งจากเซิร์ฟเวอร์เทียบกับ WebSockets” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เหตุการณ์ที่ส่งจากเซิร์ฟเวอร์เทียบกับ WebSockets”

เรียนรู้ว่าเหตุการณ์ที่ส่งจากเซิร์ฟเวอร์ให้การสตรีมแบบเรียลไทม์ทางเดียวอย่างไร เปรียบเทียบกับ WebSockets และการสำรวจแบบยาวอย่างไร และควรเลือกเทคโนโลยีใดในสถานการณ์ใด คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “เหตุการณ์ที่ส่งจากเซิร์ฟเวอร์เทียบกับ WebSockets” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม

ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจการสื่อสารแบบเรียลไทม์
  2. WebSockets เทียบกับการสำรวจ HTTP
  3. พื้นฐานของโพรโทคอล WebSocket
  4. เหตุการณ์ที่ส่งจากเซิร์ฟเวอร์เทียบกับ WebSockets
← กลับไปที่ WebSockets & Real-Time Systems with Spring