mc0de
Advanced HTML · Lesson 13 of 13 · Final Lesson

Server-Sent Events

Bring your websites to life with real-time updates! Discover how servers push new information to browsers and build a live notification experience.

EventSource APILive Updates Event StreamsFinal Challenge
● ● ●   live-updates.js
const stream =
  new EventSource("/events");

stream.onmessage = (event) => {
  console.log(event.data);
};
LIVE EVENT STREAM
● Update received · New notification
● Update received · Status changed
● Update received · New activity
01 / Getting Started

What Are Server-Sent Events?

Imagine watching a live sports score, tracking an order, or receiving notifications without refreshing the webpage. New information appears as soon as the server sends it.

Server-Sent Events (SSE) allow a server to continuously send updates to a browser over an HTTP connection.

Unlike repeatedly requesting updates, a browser can keep an SSE connection open and listen for new messages.

Remember: SSE is primarily one-way communication: server to browser. If your application needs frequent two-way communication, WebSockets may be a better fit.
02 / The EventSource API

Connect to a Live Event Stream

JavaScript provides the EventSource interface for receiving SSE messages.

Connecting to an SSE endpoint
const stream = new EventSource("/events");

stream.onmessage = function(event) {
  console.log("New update:", event.data);
};

The browser connects to the /events endpoint and listens for incoming messages. The endpoint must exist on a server and respond with an SSE stream.

The event.data property contains the message content sent by the server.

03 / Server Message Format

What Does an SSE Message Look Like?

The server sends text using the text/event-stream content type. Each event is separated by a blank line.

Example server response
data: Welcome to the live stream!

data: A new notification has arrived!

event: status
data: {"online":true}

id: 42
data: Message with an event ID

The data: field contains the message, event: can name a custom event type, and id: identifies an event to support reconnection.

Developer tip: SSE uses UTF-8 text. Multiple consecutive data: lines can form a single message.
04 / Custom Events

Listen for Different Kinds of Updates

Not every update has to be a generic message. The server can name events, and your JavaScript can respond differently.

Listening for a named event
const stream = new EventSource("/events");

stream.addEventListener("status", function(event) {
  const update = JSON.parse(event.data);
  console.log("Online:", update.online);
});

Named events are useful when your application receives several types of information, such as notifications, progress reports, and system status updates.

05 / Connection Management

Open, Error, and Close

Real connections can fail. SSE provides events and methods to help you manage the connection.

Managing a stream
const stream = new EventSource("/events");

stream.onopen = function() {
  console.log("Connected!");
};

stream.onerror = function() {
  console.log("Connection interrupted.");
};

// When you no longer need updates:
stream.close();

The browser normally attempts to reconnect automatically after an interrupted SSE connection. Calling close() stops that behavior for the current EventSource instance.

The readyState property indicates whether a connection is connecting, open, or closed.

06 / Interactive Demonstration

Try a Live Notification Stream

Click Start Stream to simulate new server updates arriving every few seconds. Pause the stream or clear the activity feed whenever you like.

📡 Live Notification Center
Disconnected · 0 messages received
Waiting for updates…
What’s happening? This demonstration uses a JavaScript timer to imitate incoming server messages. It doesn’t open a real SSE connection or contact a server.
07 / SSE Best Practices

Build Reliable Live Experiences

  • Use SSE when the server needs to push updates to the browser.
  • Use HTTPS and properly authenticate protected endpoints.
  • Handle connection interruptions and reconnection states.
  • Close streams when they’re no longer needed.
  • Use event IDs when clients may need to resume missed updates.
  • Validate and safely display received data; avoid inserting untrusted content as HTML.
  • Remember that browser and server connection limits can affect your design.
  • Use WebSockets when full-duplex communication is required.

SSE is particularly useful for live dashboards, progress tracking, notifications, and activity feeds.

🚀 Final mC0de Challenge

Build a Live Notification Center

Put your JavaScript skills to work by creating an interface that simulates real-time messages arriving from a server.

Your Mission

  1. Create a notification center with a heading and activity feed.
  2. Add a Start Stream button.
  3. Use setInterval() to simulate incoming events.
  4. Add each message to the activity feed.
  5. Display the number of messages received.
  6. Add a Stop Stream button that uses clearInterval().
  7. Add a Clear Feed button to remove previous messages.
Extra challenge: Give each notification a category, such as News, Alert, or System, and display the category beside the message.
💻 Interactive HTML Sandbox

Build Your Live Event Stream!

Edit the HTML and JavaScript, then click Run Code. Use Load Solution to explore a complete notification center with a live feed, timestamps, and connection controls.

Live Result
Sandbox note: The activity feed is a simulation. To use actual EventSource messages, your application needs a working SSE server endpoint.
Lesson 13 Recap

What You’ve Learned

You’ve Reached the Final Lesson!

You’ve learned how live information travels from a server to a browser and how to build interfaces that respond to incoming updates.

  • What Server-Sent Events are and when to use them.
  • How the EventSource API works.
  • How SSE messages and custom events are formatted.
  • How to manage connections and handle errors.
  • How to create a simulated live notification feed.

Congratulations on reaching the end of the Advanced HTML learning path! Mark this lesson complete below to record your achievement.

Your Learning Journey

0 / 13 Completed

Complete all 13 lessons to finish the Advanced HTML learning path!

🎉

You Made It to Lesson 13!

You've learned how to receive live updates, work with event streams, manage connections, and build a notification center. Mark your final lesson complete to record your achievement!

🏆

Advanced HTML Completed!

Congratulations! You've completed all 13 lessons in the mC0de Advanced HTML Learning Path. From text formatting and forms to Canvas, SVG, browser storage, background processing, and live updates — you've covered an incredible range of web technologies.

✓ 13 / 13 Lessons Completed

Keep building, experimenting, and turning your ideas into real projects. Your coding journey continues!