SlideShare a Scribd company logo
Introduction of Server-
Sent Events (SSE)
Yuji KONDO
What’s Server-Sent Events?
• Server-sent events (SSE) is a technology where a
browser receives automatic updates from a server
via HTTP connection. The Server-Sent Events
EventSource API is standardized as part of
HTML5[1] by the W3C.
Communication Sequence of
HTTP Request and Response
Client Server
Request
Response
Request
Request
Response
Response
Communication Sequence
of HTTP Request and SSE
Client Server
Request
Event
Event
Event
Event
Event
Practice #1
• Sample under Laravel 5.1
• Let the server to notify time in every 5 seconds and
the client displays it
routes.php
Route::get('test/monitor', ['as' => 'test.monitor', 'uses'
=> ‘TestController@monitor’]);
Route::get('test/update', ['as' => 'test.update', 'uses'
=> ‘TestController@update']);
TestController.php #1
public function monitor()
{
return view('test.monitor');
}
test.monitor.blade.php #1
<html lang="en">
<head>
<title>HTML5 Server-Sent Events</title>
<script type="text/javascript">
if (!!window.EventSource) {
var source = new EventSource('/test/update');
}
else {
// Result to xhr polling :(
}
source.addEventListener('message', function(event) {
var date = event.data;
document.getElementById("result").innerHTML += event.data + "<br>";
}, false);
test.monitor.blade.php #2
source.addEventListener('open', function(event) {
// Connection was opened.
}, false);
source.addEventListener('error', function(event) {
if (event.readyState == EventSource.CLOSED) {
// Connection was closed.
}
}, false);
</script>
</head>
<body>
<div id="result">
<!--Server response will be inserted here-->
</div>
</body>
</html>
TestController.php #2
public function update()
{
date_default_timezone_set("Asia/Manila");
header("Content-Type: text/event-stream");
header('X-Accel-Buffering: no');
ob_start();
while(true) {
echo 'data: ' . date('Y-m-d H:i:s') . "nn";
ob_flush();
flush();
sleep(5);
}
}
Practice #2
• Sample under Laravel 5.1
• Let the server notify the progress and the client
displays it on a progress bar
routes.php
Route::get('test/register', ['as' => 'test.register', 'uses'
=> ‘TestController@register']);
Route::get('test/notify', ['as' => 'test.notify', 'uses' =>
‘TestController@notify']);
TestController.php #1
public function register()
{
return view(‘test.progress');
}
test.progress.blade.php #1
<html lang="en">
<head>
<title>HTML5 Server-Sent Events</title>
<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="/jquery/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
if (!!window.EventSource) {
var source = new EventSource('/test/notify');
}
else {
// Result to xhr polling :(
}
source.addEventListener('message', function(event) {
var data = event.data.split("/");
var progress = parseInt(data[0], 10) / parseInt(data[1], 10) * 100;
$("#progress-bar").css("width", progress + "%");
if(data[0] == data[1]) {
$("#progress-bar").removeClass('active');
$("#progress-bar").removeClass('progress-bar-striped');
source.close();
}
}, false);
test.monitor.blade.php #2
source.addEventListener('open', function(event) {
// Connection was opened.
}, false);
source.addEventListener('error', function(event) {
if (event.readyState == EventSource.CLOSED) {
// Connection was closed.
}
}, false);
</script>
</head>
<body class="col-lg-offset-2 col-lg-8">
<div id="text">
Test of SSE
</div>
<div class="progress">
<div id=“progress-bar" class="progress-bar progress-bar-striped active” role=“progressbar”
aria-valuenow=“0" aria-valuemin=“0" aria-valuemax=“100" style="min-width: 0%;"
>
<span id="progress-text" class="sr-only">45% Complete</span>
</div>
</div>
</body>
</html>
TestController.php #2
public function notify()
{
date_default_timezone_set("Asia/Manila");
header("Content-Type: text/event-stream");
header('X-Accel-Buffering: no');
ob_start();
$n = 10;
for($i = 1; $i < $n+1; $i ++) {
echo 'data: ' . $i . '/' . $n . "nn";
ob_flush();
flush();
sleep(1);
}
}
What’s the benefit of SSE?
• Less Communication, Packets, and Network Load
• Comparatively Simple to Use
• Implemented Client API on Modern Browser’s
JavaScript engine
Let’s Enjoy SSE!

More Related Content

What's hot

Testing Agile Web Services from soapUI
Testing Agile Web Services from soapUITesting Agile Web Services from soapUI
Testing Agile Web Services from soapUI
PLM Mechanic .
 
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes AhmedabadJava Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
NicheTech Com. Solutions Pvt. Ltd.
 
AJAX ASP.Net
AJAX ASP.NetAJAX ASP.Net
AJAX ASP.Net
SHC
 
Introducing Revel
Introducing RevelIntroducing Revel
Introducing Revel
Zhebr
 
Programming ten commandments
Programming ten commandmentsProgramming ten commandments
Programming ten commandments
Amitai Barnea
 
Protractor Tutorial Quality in Agile 2015
Protractor Tutorial Quality in Agile 2015Protractor Tutorial Quality in Agile 2015
Protractor Tutorial Quality in Agile 2015
Andrew Eisenberg
 
Servlet LifeCycle Demo App
Servlet LifeCycle Demo  AppServlet LifeCycle Demo  App
Servlet LifeCycle Demo AppPeeyush Ranjan
 
How It Works - Load Testing
How It Works - Load TestingHow It Works - Load Testing
How It Works - Load Testing
uTest
 
Service workers and the role they play in modern day web apps
Service workers and the role they play in modern day web appsService workers and the role they play in modern day web apps
Service workers and the role they play in modern day web apps
Mukul Jain
 
Silverlight3 WCF Exceptions
Silverlight3 WCF ExceptionsSilverlight3 WCF Exceptions
Silverlight3 WCF Exceptions
William Austin
 

What's hot (10)

Testing Agile Web Services from soapUI
Testing Agile Web Services from soapUITesting Agile Web Services from soapUI
Testing Agile Web Services from soapUI
 
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes AhmedabadJava Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
 
AJAX ASP.Net
AJAX ASP.NetAJAX ASP.Net
AJAX ASP.Net
 
Introducing Revel
Introducing RevelIntroducing Revel
Introducing Revel
 
Programming ten commandments
Programming ten commandmentsProgramming ten commandments
Programming ten commandments
 
Protractor Tutorial Quality in Agile 2015
Protractor Tutorial Quality in Agile 2015Protractor Tutorial Quality in Agile 2015
Protractor Tutorial Quality in Agile 2015
 
Servlet LifeCycle Demo App
Servlet LifeCycle Demo  AppServlet LifeCycle Demo  App
Servlet LifeCycle Demo App
 
How It Works - Load Testing
How It Works - Load TestingHow It Works - Load Testing
How It Works - Load Testing
 
Service workers and the role they play in modern day web apps
Service workers and the role they play in modern day web appsService workers and the role they play in modern day web apps
Service workers and the role they play in modern day web apps
 
Silverlight3 WCF Exceptions
Silverlight3 WCF ExceptionsSilverlight3 WCF Exceptions
Silverlight3 WCF Exceptions
 

Similar to Introduction of server sent events (sse)

Intoduction to Play Framework
Intoduction to Play FrameworkIntoduction to Play Framework
Intoduction to Play Framework
Knoldus Inc.
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
Angular Js Basics
Angular Js BasicsAngular Js Basics
Angular Js Basics
أحمد عبد الوهاب
 
Live Streaming & Server Sent Events
Live Streaming & Server Sent EventsLive Streaming & Server Sent Events
Live Streaming & Server Sent Eventstkramar
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
erdemergin
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
Sunpawet Somsin
 
Ruby HTTP clients comparison
Ruby HTTP clients comparisonRuby HTTP clients comparison
Ruby HTTP clients comparisonHiroshi Nakamura
 
Performance and Scalability Testing with Python and Multi-Mechanize
Performance and Scalability Testing with Python and Multi-MechanizePerformance and Scalability Testing with Python and Multi-Mechanize
Performance and Scalability Testing with Python and Multi-Mechanize
coreygoldberg
 
Asp.net tips
Asp.net tipsAsp.net tips
Asp.net tips
actacademy
 
Automated Testing Of Web Applications Using XML
Automated  Testing Of  Web  Applications Using  XMLAutomated  Testing Of  Web  Applications Using  XML
Automated Testing Of Web Applications Using XML
diongillard
 
Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"
Fwdays
 
ASP.NET MVC 4 Request Pipeline Internals
ASP.NET MVC 4 Request Pipeline InternalsASP.NET MVC 4 Request Pipeline Internals
ASP.NET MVC 4 Request Pipeline Internals
Lukasz Lysik
 
Asp Net Architecture
Asp Net ArchitectureAsp Net Architecture
Asp Net Architecture
Juan Jose Gonzalez Faundez
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node js
Francois Zaninotto
 
Developing ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller PatternDeveloping ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller Pattern
goodfriday
 
QSpiders - Installation and Brief Dose of Load Runner
QSpiders - Installation and Brief Dose of Load RunnerQSpiders - Installation and Brief Dose of Load Runner
QSpiders - Installation and Brief Dose of Load Runner
Qspiders - Software Testing Training Institute
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
yuvalb
 
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
Lokesh Singrol
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
Peter Gfader
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep dive
Axilis
 

Similar to Introduction of server sent events (sse) (20)

Intoduction to Play Framework
Intoduction to Play FrameworkIntoduction to Play Framework
Intoduction to Play Framework
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Angular Js Basics
Angular Js BasicsAngular Js Basics
Angular Js Basics
 
Live Streaming & Server Sent Events
Live Streaming & Server Sent EventsLive Streaming & Server Sent Events
Live Streaming & Server Sent Events
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
 
Ruby HTTP clients comparison
Ruby HTTP clients comparisonRuby HTTP clients comparison
Ruby HTTP clients comparison
 
Performance and Scalability Testing with Python and Multi-Mechanize
Performance and Scalability Testing with Python and Multi-MechanizePerformance and Scalability Testing with Python and Multi-Mechanize
Performance and Scalability Testing with Python and Multi-Mechanize
 
Asp.net tips
Asp.net tipsAsp.net tips
Asp.net tips
 
Automated Testing Of Web Applications Using XML
Automated  Testing Of  Web  Applications Using  XMLAutomated  Testing Of  Web  Applications Using  XML
Automated Testing Of Web Applications Using XML
 
Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"
 
ASP.NET MVC 4 Request Pipeline Internals
ASP.NET MVC 4 Request Pipeline InternalsASP.NET MVC 4 Request Pipeline Internals
ASP.NET MVC 4 Request Pipeline Internals
 
Asp Net Architecture
Asp Net ArchitectureAsp Net Architecture
Asp Net Architecture
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node js
 
Developing ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller PatternDeveloping ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller Pattern
 
QSpiders - Installation and Brief Dose of Load Runner
QSpiders - Installation and Brief Dose of Load RunnerQSpiders - Installation and Brief Dose of Load Runner
QSpiders - Installation and Brief Dose of Load Runner
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep dive
 

Recently uploaded

Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 

Recently uploaded (20)

Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 

Introduction of server sent events (sse)

  • 1. Introduction of Server- Sent Events (SSE) Yuji KONDO
  • 2. What’s Server-Sent Events? • Server-sent events (SSE) is a technology where a browser receives automatic updates from a server via HTTP connection. The Server-Sent Events EventSource API is standardized as part of HTML5[1] by the W3C.
  • 3. Communication Sequence of HTTP Request and Response Client Server Request Response Request Request Response Response
  • 4. Communication Sequence of HTTP Request and SSE Client Server Request Event Event Event Event Event
  • 5. Practice #1 • Sample under Laravel 5.1 • Let the server to notify time in every 5 seconds and the client displays it
  • 6. routes.php Route::get('test/monitor', ['as' => 'test.monitor', 'uses' => ‘TestController@monitor’]); Route::get('test/update', ['as' => 'test.update', 'uses' => ‘TestController@update']);
  • 7. TestController.php #1 public function monitor() { return view('test.monitor'); }
  • 8. test.monitor.blade.php #1 <html lang="en"> <head> <title>HTML5 Server-Sent Events</title> <script type="text/javascript"> if (!!window.EventSource) { var source = new EventSource('/test/update'); } else { // Result to xhr polling :( } source.addEventListener('message', function(event) { var date = event.data; document.getElementById("result").innerHTML += event.data + "<br>"; }, false);
  • 9. test.monitor.blade.php #2 source.addEventListener('open', function(event) { // Connection was opened. }, false); source.addEventListener('error', function(event) { if (event.readyState == EventSource.CLOSED) { // Connection was closed. } }, false); </script> </head> <body> <div id="result"> <!--Server response will be inserted here--> </div> </body> </html>
  • 10. TestController.php #2 public function update() { date_default_timezone_set("Asia/Manila"); header("Content-Type: text/event-stream"); header('X-Accel-Buffering: no'); ob_start(); while(true) { echo 'data: ' . date('Y-m-d H:i:s') . "nn"; ob_flush(); flush(); sleep(5); } }
  • 11. Practice #2 • Sample under Laravel 5.1 • Let the server notify the progress and the client displays it on a progress bar
  • 12. routes.php Route::get('test/register', ['as' => 'test.register', 'uses' => ‘TestController@register']); Route::get('test/notify', ['as' => 'test.notify', 'uses' => ‘TestController@notify']);
  • 13. TestController.php #1 public function register() { return view(‘test.progress'); }
  • 14. test.progress.blade.php #1 <html lang="en"> <head> <title>HTML5 Server-Sent Events</title> <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <script type="text/javascript" src="/jquery/jquery-2.2.4.min.js"></script> <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript"> if (!!window.EventSource) { var source = new EventSource('/test/notify'); } else { // Result to xhr polling :( } source.addEventListener('message', function(event) { var data = event.data.split("/"); var progress = parseInt(data[0], 10) / parseInt(data[1], 10) * 100; $("#progress-bar").css("width", progress + "%"); if(data[0] == data[1]) { $("#progress-bar").removeClass('active'); $("#progress-bar").removeClass('progress-bar-striped'); source.close(); } }, false);
  • 15. test.monitor.blade.php #2 source.addEventListener('open', function(event) { // Connection was opened. }, false); source.addEventListener('error', function(event) { if (event.readyState == EventSource.CLOSED) { // Connection was closed. } }, false); </script> </head> <body class="col-lg-offset-2 col-lg-8"> <div id="text"> Test of SSE </div> <div class="progress"> <div id=“progress-bar" class="progress-bar progress-bar-striped active” role=“progressbar” aria-valuenow=“0" aria-valuemin=“0" aria-valuemax=“100" style="min-width: 0%;" > <span id="progress-text" class="sr-only">45% Complete</span> </div> </div> </body> </html>
  • 16. TestController.php #2 public function notify() { date_default_timezone_set("Asia/Manila"); header("Content-Type: text/event-stream"); header('X-Accel-Buffering: no'); ob_start(); $n = 10; for($i = 1; $i < $n+1; $i ++) { echo 'data: ' . $i . '/' . $n . "nn"; ob_flush(); flush(); sleep(1); } }
  • 17. What’s the benefit of SSE? • Less Communication, Packets, and Network Load • Comparatively Simple to Use • Implemented Client API on Modern Browser’s JavaScript engine