---
title: "What is SSE in HTML5? And also describe the concept, how to automatically gets update a webpage from a server?"  
description: "What is SSE in HTML5? And also describe the concept, how to automatically gets update a webpage from a server?"  
author: "Gurmeet Kaur"  
published: 2018-12-13  
canonical: https://answers.mindstick.com/qa/51749/what-is-sse-in-html5-and-also-describe-the-concept-how-to-automatically-gets-update-a-webpage-from-a-server  
category: "programming"  
tags: ["html5", "api", "events", "update webpage"]  
reading_time: 2 minutes  

---

# What is SSE in HTML5? And also describe the concept, how to automatically gets update a webpage from a server?

What is [SSE](https://www.mindstick.com/interview/34423/what-is-sse) in [HTML5](https://www.mindstick.com/articles/1535/offline-add-edit-delete-data-in-html5-indexeddb)? And also [describe](https://www.mindstick.com/forum/158512/how-does-a-decision-tree-algorithm-work-describe-the-process-of-building-a-decision-tree) the [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net), how to automatically gets [update](https://www.mindstick.com/forum/156355/what-is-google-pigeon-update) a webpage from a [server](https://www.mindstick.com/articles/710/apache-server-installation-on-windows)?

## Answers

### Answer by Arti Mishra

**"SSE(Server-Sent Event) "** Server-Sent Event provides a mechanism By which, we can **send the response from a server without any request.** Because in the earlier time when the **user asked some updates from a server (that’s called** **pooling) then this mechanism isn’t easily handled by the users,** and for removing the burden of pooling html5 added the concept of SSE(Server-Sent Event). \
![What is SSE in HTML5? And also describe the concept, how to automatically gets update a webpage from a server?](https://answers.mindstick.com/questionanswer/15398e0f-87b2-4d0d-b333-6d59b3dc44e2/images/d54280d3-2e22-41b6-9601-c9e79c8fa35e.jpeg)\
\
Server-Sent Event is a technology that **enabling a browser when a web page gets an update automatically from a server.** And in present time, a real example of SSE is- facebook /twitter updates, news feeds update, Gmail updates….etc. \
**Event of SSE :** It mainly has three events. And it’s called when-

- **onopen** --- use for connection establish.
- **onmessage**--- when any updates are available.
- **onerror** --- when any error occurs.

\
**Properties of SSE :**

- event.data
- event.message

**\** **NOTE:**

- **For SSE, on Server site, we must set the**

Content-Type:text/event-stream

Cache-control:no-cache

- During the response from the server, output must be prefixed with **“data :” string** (I mean **information always starts from “data:” string**).
- After set the data you must **add a flush() method** for flushing the data and data go back to the web page.

**Here we implement the simple example to gets the server response when a webpage is loaded.** **\**

```
<html><head><title> SSE Example</title><script>function check_updates(){if(typeof(EventSource) !== "undefined") { var source = new EventSource("server.php");  source.onmessage = function(event) {    document.getElementById("result").innerHTML += event.data + "<br>";  };} else {  document.getElementById("result").innerHTML = "Sorry, Your Browser doesn't support SSE...";}}</script>
</head><body onload="check_updates();"><h1>Get the updates from a Server</h1><div id="result"></div></body></html>
```

**\** **Server Site Code :** **If you are using PHP language then write the below code.** **\**

```
<?phpheader('Content-Type: text/event-stream');header('Cache-Control: no-cache');
$currentTime = date('r');echo "data:Now, The server time is: {$ currentTime } \n";
flush();?>
```

**\** **If you are using ASP.NET language then write the below code.**

```
<%Response.ContentType = "text/event-stream"Response.Expires = -1Response.Write("data:Now The server time is: " & now())Response.Flush()%>
```

\
\
\
**"Thanks!!! for Reading"**


---

Original Source: https://answers.mindstick.com/qa/51749/what-is-sse-in-html5-and-also-describe-the-concept-how-to-automatically-gets-update-a-webpage-from-a-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
