SlideShare a Scribd company logo
1 of 62
Download to read offline
Server-Sent Events
Software developer, father, geek, PHP expert.
I also write in Java, Kotlin, NodeJs. For me, the
language is not a problem I prefer to focus on
the software architecture. Open-source
enthusiast, active contributor on GitHub and
Stackover
fl
ow.
Chi sono
Alessandro Minoccheri
Team leader & Software developer
@minompi
AlessandroMinoccheri
True Life Story
_
Constraints
• Time:
Delivery release 1 in a few month
s

• Code Quality
Maintainability code with good coverage
 

• Auction engine
Auction engine
• Auction common behaviours
starts and stops without delays
• Different type of bid:
buy now, bid, automatic bi
d

• Push notifications
to update all clients subscribed to a specific channel
 

• Other features
Solutions to build an auction engine in a short time
• Adding a new micro service in NodeJ
S

• Adding the business logic into Next.j
s

• Using Websocke
t

• Using Server-sent events
Cognitive Bias
A cognitive bias is a systematic
pattern of deviation from norm or
rationality in judgment.
Individuals create their own
"subjective reality" from their
perception of the input.
An individual's construction of
reality, not the objective input,
may dictate their behaviours in the
world.
Image from: https://www.verywellmind.com/what-is-a-con
fi
rmation-bias-2795024
What is the Server-Sent
events approach?
_
Server-Sent Events (SSE) is a server push technology enabling a client to
receive automatic updates from a server via an HTTP connection.
It was born on September 1, 2006.
It provides a memory-efficient implementation of XHR streaming. Unlike a
raw XHR connection, which buffers the full received response until the
connection is dropped, an SSE connection can discard processed messages
without accumulating all of them in memory.
Difference between
WebSocket approach
_
Image from https://www.onlyfullstack.com/polling-vs-server-sent-events-vs-websocket/
Image from https://www.onlyfullstack.com/polling-vs-server-sent-events-vs-websocket/
Websocket Vs Server-Sent
events
_
Websocket
• client <—> server, bidirectional communication.
 

• Create a TCP connection to the server, and keep it open as long as needed.
 

• The server or client can easily close the connection.
 

• The client goes through an HTTP compatible handshake process
.

• If it succeeds, then the server and client can exchange data in both
directions at any time
.

• Uses websocket protocol
Server-Sent Events
• request --> wait --> server can send events, unidirectional communication.
 

• Creates a connection to the server, but maintains a keep-alive connection
open for some time (not long though).
 

• Uses HTTP protocol
Websocket Advantages
• Bi-directional communication in realtime, connections can both send and
receive data from the browser (chat example
)

• Generally do not use ‘XMLHttpRequest' and headers are not sent every-
time so it reduces the expensive data loads being sent to the server
.

• Can transmit both binary data and UTF-
8

• Browser support compatibility is more with WebSockets.
Websocket limits
• When connections are terminated WebSockets don’t automatically recover, you
need to implement it by yoursel
f

• Some enterprise firewalls with packet inspection have trouble dealing with i
t

• Browsers that are older than 2011 do not support WebSocket connection
s

• WebSockets are more complex and task-demanding to set up. Because it requires
a ton of upfront wor
k

• It cannot be placed behind container-based authentication methods with header-
based security, it has to be query parameters (or hopefully, a token-based
authentication where the token is established outside the WebSocket path).
SSE Advantages
• Transported over simple HTTP instead of a custom protoco
l

• Simpler protoco
l

• Built in support for re-connection and event-i
d

• No trouble with corporate firewalls doing packet inspection
SSE ideal cases
• Stock update
s

• Twitter feed updatin
g

• Notifications to browser
SSE limits
• It’s limited to UTF-8, and does not support binary data
.

• When not used over HTTP/2, Server-Sent events approach is subject to
limitations with regard to the maximum number of open connections. This
can be especially painful when opening various tabs as the limit is per
browser and set to a very low number (6)
.

• SSE is mono-directiona
l

• Not native browsers support
Browser Supports
Another notable difference is the browser compatibility of the two technologies.
More browsers support WebSockets natively than SSEs.
However, there are available workaround with poly-fill (replicate an API) in
Javascript that simulate the SSE functionality to solve this issue.
All modern browsers support server-sent events: Firefox 6+, Google Chrome
6+, Opera 11.5+, Safari 5+, Microsoft Edge 79+
.
We choose Server-Sent Events and
symfony/mercure for the implementation
_
The symfony/mercure component
implements the “publisher" part of the
Mercure Protocol
Let’s try to implement it from scratch
https://github.com/AlessandroMinoccheri/sse-poc
_
Installation via composer
Mercure container
Configuration
Implementation
Backend
SymfonyComponentMercureUpdate
SymfonyComponentMercureMessengerUpdateHandler
SymfonyComponentMercureHubInterface
SymfonyComponentMercureHub
Implementation Frontend
Implementation Frontend
Opening page
Observing the network
Offer
Observing the network
Updated page automatically
Observing the network
Now we can complicate the
system
_
How to send an event to a specific user?
How to send an event to a specific user?
Async dispatching
Async dispatching is discouraged.
Most Mercure hubs already handle publications asynchronously and using Messenger is
usually not necessary
.

Instead of calling the Publisher service directly, you can also let Symfony dispatching the
updates asynchronously thanks to the provided integration with the Messenger component
.

First, be sure to install the Messenger component and to configure properly a transport (if
you don't, the handler will be called synchronously)
.
Async dispatching
Possible problems
How to catch mercure connection problems?
• Add an health check system for you mercure containe
r

• Add logs to your frontend (Sentry
)

• Add logs to your backend
How did we implement it?
_
Real Implementation
Real Implementation
Implementation inside the Processor
How to test it?
_
Using a mock
Using stub class
Send new tweet to all clients
subscribed
_
Service class
• Frontend client can subscribe to different topic
s

• Backend can send, via mercure, events to a
specific topi
c

• Frontend receive events inside the subscribe
connection request
Recap
• SSE is very useful for specific problem
s

• Easy to star
t

• Small configuratio
n

• It’s in production
Summary

More Related Content

Similar to ServerSentEventsV2.pdf

Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - TalkMatthias Noback
 
Building and Scaling a WebSockets Pubsub System
Building and Scaling a WebSockets Pubsub SystemBuilding and Scaling a WebSockets Pubsub System
Building and Scaling a WebSockets Pubsub SystemKapil Reddy
 
How to build a SaaS solution in 60 days
How to build a SaaS solution in 60 daysHow to build a SaaS solution in 60 days
How to build a SaaS solution in 60 daysBrett McLain
 
Mulesoft Pune Meetup Deck - Apr 2020
Mulesoft Pune Meetup Deck - Apr 2020Mulesoft Pune Meetup Deck - Apr 2020
Mulesoft Pune Meetup Deck - Apr 2020Santosh Ojha
 
Enhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketEnhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketMauricio "Maltron" Leal
 
Real-Time with Flowdock
Real-Time with FlowdockReal-Time with Flowdock
Real-Time with FlowdockFlowdock
 
Applying microservices principles to front end
Applying microservices principles to front endApplying microservices principles to front end
Applying microservices principles to front endAhmed Elharouny
 
Docker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platformsDocker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platformsFederico Michele Facca
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivitypkaviya
 
IoT with SignalR & .NET Gadgeteer - NetMF@Work
IoT with SignalR & .NET Gadgeteer - NetMF@WorkIoT with SignalR & .NET Gadgeteer - NetMF@Work
IoT with SignalR & .NET Gadgeteer - NetMF@WorkMirco Vanini
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersViktor Gamov
 
Introduction to SignalR
Introduction to SignalRIntroduction to SignalR
Introduction to SignalRAdam Mokan
 
Web Sockets are not Just for Web Browsers
Web Sockets are not Just for Web BrowsersWeb Sockets are not Just for Web Browsers
Web Sockets are not Just for Web Browserscjmyers
 
Do You Need A Service Mesh?
Do You Need A Service Mesh?Do You Need A Service Mesh?
Do You Need A Service Mesh?NGINX, Inc.
 
[OWASP Poland Day] A study of Electron security
[OWASP Poland Day] A study of Electron security[OWASP Poland Day] A study of Electron security
[OWASP Poland Day] A study of Electron securityOWASP
 
IBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New TricksIBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New TricksDejan Glozic
 
Monitoring in Big Data Platform - Albert Lewandowski, GetInData
Monitoring in Big Data Platform - Albert Lewandowski, GetInDataMonitoring in Big Data Platform - Albert Lewandowski, GetInData
Monitoring in Big Data Platform - Albert Lewandowski, GetInDataGetInData
 

Similar to ServerSentEventsV2.pdf (20)

Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
 
Building and Scaling a WebSockets Pubsub System
Building and Scaling a WebSockets Pubsub SystemBuilding and Scaling a WebSockets Pubsub System
Building and Scaling a WebSockets Pubsub System
 
How to build a SaaS solution in 60 days
How to build a SaaS solution in 60 daysHow to build a SaaS solution in 60 days
How to build a SaaS solution in 60 days
 
Mulesoft Pune Meetup Deck - Apr 2020
Mulesoft Pune Meetup Deck - Apr 2020Mulesoft Pune Meetup Deck - Apr 2020
Mulesoft Pune Meetup Deck - Apr 2020
 
Enhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketEnhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocket
 
Real-Time with Flowdock
Real-Time with FlowdockReal-Time with Flowdock
Real-Time with Flowdock
 
Signal R 2015
Signal R 2015Signal R 2015
Signal R 2015
 
Applying microservices principles to front end
Applying microservices principles to front endApplying microservices principles to front end
Applying microservices principles to front end
 
Docker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platformsDocker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platforms
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
 
Servlet programming
Servlet programmingServlet programming
Servlet programming
 
IoT with SignalR & .NET Gadgeteer - NetMF@Work
IoT with SignalR & .NET Gadgeteer - NetMF@WorkIoT with SignalR & .NET Gadgeteer - NetMF@Work
IoT with SignalR & .NET Gadgeteer - NetMF@Work
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
 
Introduction to SignalR
Introduction to SignalRIntroduction to SignalR
Introduction to SignalR
 
Web Sockets are not Just for Web Browsers
Web Sockets are not Just for Web BrowsersWeb Sockets are not Just for Web Browsers
Web Sockets are not Just for Web Browsers
 
Do You Need A Service Mesh?
Do You Need A Service Mesh?Do You Need A Service Mesh?
Do You Need A Service Mesh?
 
JAVA
JAVAJAVA
JAVA
 
[OWASP Poland Day] A study of Electron security
[OWASP Poland Day] A study of Electron security[OWASP Poland Day] A study of Electron security
[OWASP Poland Day] A study of Electron security
 
IBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New TricksIBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New Tricks
 
Monitoring in Big Data Platform - Albert Lewandowski, GetInData
Monitoring in Big Data Platform - Albert Lewandowski, GetInDataMonitoring in Big Data Platform - Albert Lewandowski, GetInData
Monitoring in Big Data Platform - Albert Lewandowski, GetInData
 

More from Alessandro Minoccheri (8)

Kotlin hexagonal-architecture
Kotlin hexagonal-architectureKotlin hexagonal-architecture
Kotlin hexagonal-architecture
 
Hexagonal architecture ita
Hexagonal architecture itaHexagonal architecture ita
Hexagonal architecture ita
 
Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
Smart working
Smart workingSmart working
Smart working
 
Symfony aws
Symfony awsSymfony aws
Symfony aws
 
Composer
ComposerComposer
Composer
 
Doctrine
DoctrineDoctrine
Doctrine
 
Symfony4
Symfony4Symfony4
Symfony4
 

Recently uploaded

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

ServerSentEventsV2.pdf