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.
const stream =
new EventSource("/events");
stream.onmessage = (event) => {
console.log(event.data);
};
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.
Connect to a Live Event Stream
JavaScript provides the EventSource interface for receiving SSE messages.
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.
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.
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.
data: lines can form a single message.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.
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.
Open, Error, and Close
Real connections can fail. SSE provides events and methods to help you manage the connection.
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.
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.
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.
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
- Create a notification center with a heading and activity feed.
- Add a Start Stream button.
- Use
setInterval()to simulate incoming events. - Add each message to the activity feed.
- Display the number of messages received.
- Add a Stop Stream button that uses
clearInterval(). - Add a Clear Feed button to remove previous messages.
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.
EventSource messages, your application needs a working SSE server endpoint.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
EventSourceAPI 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 CompletedComplete 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 CompletedKeep building, experimenting, and turning your ideas into real projects. Your coding journey continues!


