Introduction
In some web applications we need to show real time data to end users. Means if any changes occurs(new data available) in server, it needs to show end user. For instance, you are doing chat in Facebook in one tab of your browser. You opened another tab in same browser and send an a message to same user(with whom you doing chat in previous chat). You will see that message will appear in both the tabs and it is called real-time push.
In order to accomplish above functionality, client sends interval basis AJAX request to server to check if data available. ServerSentEvents(SSE) api which helps server will push data to client when data is available in server.
What is Server Sent Events?
SSE is an acronym and stands for Server Sent Events. It is available in HTML5 EventSource JavaScript API. It allows a web page to get updates from a server when any changes occurs in server. It is mostly supported by latest browsers except Internet Explorer(IE).
Using code
We are going to implement requirement like: there is a link button and click on it, it displays current time in each second interval basis.
In order to achieve the same, we need to add following action in HomeController. It sets response content type as text/event-stream. Next it loops over date and flushes data to browser.
Once we are done with server side implementation, it's time to add code in client side to receive data from server and displays it.
Figure1: Displays current time
Conclusion
Here we discussed about SSE(Server Sent Events). It is very important API available in HTML5. It helps to push data from server to client when any changes occurs in server side. If you want to use a bidirectional communication channel you can use the HTML5 Web Sockets API. Disadvantage of SSE is it browser dependent. If browser doesn't support SSE then user can't see data, but it is easy to use it. You can also use SignalR for realtime pushing data to end user.
Hope this helps.
In some web applications we need to show real time data to end users. Means if any changes occurs(new data available) in server, it needs to show end user. For instance, you are doing chat in Facebook in one tab of your browser. You opened another tab in same browser and send an a message to same user(with whom you doing chat in previous chat). You will see that message will appear in both the tabs and it is called real-time push.
In order to accomplish above functionality, client sends interval basis AJAX request to server to check if data available. ServerSentEvents(SSE) api which helps server will push data to client when data is available in server.
What is Server Sent Events?
SSE is an acronym and stands for Server Sent Events. It is available in HTML5 EventSource JavaScript API. It allows a web page to get updates from a server when any changes occurs in server. It is mostly supported by latest browsers except Internet Explorer(IE).
Using code
We are going to implement requirement like: there is a link button and click on it, it displays current time in each second interval basis.
In order to achieve the same, we need to add following action in HomeController. It sets response content type as text/event-stream. Next it loops over date and flushes data to browser.
- public void Message()
- {
- Response.ContentType = "text/event-stream";
- DateTime startDate = DateTime.Now;
- while (startDate.AddMinutes(1) > DateTime.Now)
- {
- Response.Write(string.Format("data: {0}\n\n", DateTime.Now.ToString()));
- Response.Flush();
- System.Threading.Thread.Sleep(1000);
- }
- Response.Close();
- }
First it adds a href link which calls initialize() method to implement SSE. Second it declares a div where data will display. Thirdly, it implements Server Sent Events(SSE) through javascript with below steps.
- In first step it checks whether SSE is available in browser. If it is null then alert to end user to use other browser.
- In second setp, if SSE is available then it create EventSource object with passing URL as parameter. Next it inject following events.
- onopen it calls when connection is opened to server
- onmessage it calls when browser gets any message from server
- onclose it calls when server closes the connection.
- <a href="javascript:initialize();" >Click Me To See Magic</a>
- <div id="targetDiv"></div>
- <script>
- function initialize() {
- alert("called");
- if (window.EventSource == undefined) {
- // If not supported
- document.getElementById('targetDiv').innerHTML = "Your browser doesn't support Server Sent Events.";
- return;
- } else {
- var source = new EventSource('../Home/Message');
- source.onopen = function (event) {
- document.getElementById('targetDiv').innerHTML += 'Connection Opened.<br>';
- };
- source.onerror = function (event) {
- if (event.eventPhase == EventSource.CLOSED) {
- document.getElementById('targetDiv').innerHTML += 'Connection Closed.<br>';
- }
- };
- source.onmessage = function (event) {
- document.getElementById('targetDiv').innerHTML += event.data + '<br>';
- };
- }
- }
- </script>
Conclusion
Here we discussed about SSE(Server Sent Events). It is very important API available in HTML5. It helps to push data from server to client when any changes occurs in server side. If you want to use a bidirectional communication channel you can use the HTML5 Web Sockets API. Disadvantage of SSE is it browser dependent. If browser doesn't support SSE then user can't see data, but it is easy to use it. You can also use SignalR for realtime pushing data to end user.
Hope this helps.
No comments:
Post a Comment