SlideShare a Scribd company logo
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
_
ServerSentEventsV2.pdf
ServerSentEventsV2.pdf
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+
.
ServerSentEventsV2.pdf
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
ServerSentEventsV2.pdf
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

PDF
ServerSentEvents.pdf
PPTX
Fight empire-html5
PDF
Unlocking Realtime Web Applications - 4Developers Katowice 2023
PPTX
Websocket vs SSE - Paris.js - 24/06/15
PDF
Server Sent Events
PDF
Server-Sent Events (real-time HTTP push for HTML5 browsers)
PDF
What is a WebSocket? Real-Time Communication in Applications
PDF
Con fess 2013-sse-websockets-json-bhakti
ServerSentEvents.pdf
Fight empire-html5
Unlocking Realtime Web Applications - 4Developers Katowice 2023
Websocket vs SSE - Paris.js - 24/06/15
Server Sent Events
Server-Sent Events (real-time HTTP push for HTML5 browsers)
What is a WebSocket? Real-Time Communication in Applications
Con fess 2013-sse-websockets-json-bhakti

Similar to ServerSentEventsV2.pdf (20)

PPTX
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
PDF
Role of Server-Sent Events in Reactive Programming
PDF
Server-Side Programming Primer
PPTX
Behind the scenes of Real-Time Notifications
PPT
ODP
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
PPTX
Html5 server events
PDF
HTML5 Server Sent Events/JSF JAX 2011 Conference
PDF
4Developers 2018: Real-time capabilities in ASP.NET Core web applications (To...
PDF
Building real time applications with Symfony2
PDF
WebSocket in Enterprise Applications 2015
PPTX
Codecamp Iasi-26 nov 2011 - Html 5 WebSockets
PPTX
Codecamp iasi-26 nov 2011-web sockets
PDF
Real-Time with Flowdock
PPTX
Web Real-time Communications
PPTX
Basic understanding of websocket and and REST API
PPTX
Training Webinar: Enterprise application performance with server push technol...
PDF
Using Communication and Messaging API in the HTML5 World
PDF
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
PDF
Decoding real time web communication
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
Role of Server-Sent Events in Reactive Programming
Server-Side Programming Primer
Behind the scenes of Real-Time Notifications
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Html5 server events
HTML5 Server Sent Events/JSF JAX 2011 Conference
4Developers 2018: Real-time capabilities in ASP.NET Core web applications (To...
Building real time applications with Symfony2
WebSocket in Enterprise Applications 2015
Codecamp Iasi-26 nov 2011 - Html 5 WebSockets
Codecamp iasi-26 nov 2011-web sockets
Real-Time with Flowdock
Web Real-time Communications
Basic understanding of websocket and and REST API
Training Webinar: Enterprise application performance with server push technol...
Using Communication and Messaging API in the HTML5 World
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
Decoding real time web communication
Ad

More from Alessandro Minoccheri (8)

PDF
Kotlin hexagonal-architecture
PDF
Hexagonal architecture ita
PDF
Hexagonal architecture
PDF
Smart working
PDF
Kotlin hexagonal-architecture
Hexagonal architecture ita
Hexagonal architecture
Smart working
Ad

Recently uploaded (20)

PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
A Presentation on Artificial Intelligence
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Monthly Chronicles - July 2025
Per capita expenditure prediction using model stacking based on satellite ima...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
A Presentation on Artificial Intelligence
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Understanding_Digital_Forensics_Presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
20250228 LYD VKU AI Blended-Learning.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Building Integrated photovoltaic BIPV_UPV.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

ServerSentEventsV2.pdf