Server-Sent Events in Java: How to Send Real-Time Data Without WebSocket

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Server-Sent Events in Java: When WebSocket Is Overkill and HTTP Is Not Enough

\n\n

Have you ever wondered how Twitter news feeds, Telegram notifications, or live sports match results work? Data seems to arrive on its own, without refreshing the page. The secret is simple: Server-Sent Events (SSE) — a technology that allows the server to send data to the client in real time over a regular HTTP connection.

\n\n

In this article, we will explore Server-Sent Events in Java. You will learn why SSE is often more convenient than WebSocket, how to set up a server using the Java Servlet API, and how to receive events on the client side. The code will be functional, the style lively, and the examples as close to real-world tasks as possible.

\n\n

What Are Server-Sent Events and Why Do You Need Them?

\n\n

Server-Sent Events (SSE) is a W3C standard that allows the server to "push" data to the client over a single long-lived HTTP connection. The client subscribes to a stream of events, and the server sends messages as they occur.

\n\n

SSE is ideal for:

\n
    \n
  • Notifications — new emails, messages, status changes.
  • \n
  • News feeds — posts, tweets, currency exchange rates.
  • \n
  • Monitoring — logs, server metrics, task progress.
  • \n
  • Live data — stock quotes, match results, weather.
  • \n
\n\n

Unlike WebSocket, SSE works only in one direction (server → client), but this is sufficient for most scenarios. Plus, SSE uses regular HTTP, requires no additional ports, and easily passes through proxies and firewalls.

\n\n

Server-Sent Events vs WebSocket: Which One to Choose?

\n\n

If you only need to send data from the server to the client (without feedback), choose SSE. It is simpler, lighter, and cheaper. WebSocket is justified when the client also actively sends data (e.g., in chats or online games).

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
CriterionServer-Sent EventsWebSocket
DirectionServer → ClientBidirectional
ProtocolHTTP/1.1, HTTP/2ws:// or wss://
Automatic ReconnectionYes (built into the browser)No (needs to be implemented)
Implementation ComplexityLowMedium/High
Browser SupportAll modern browsersAll modern browsers
\n\n

For the PythonLib educational website, we chose Java because it is a reliable language for server-side development. Let's go!

\n\n

Server Side: Implementing SSE in Java Using the Servlet API

\n\n

Let's start with the server. The simplest way is to use standard Java Servlets. We need to:

\n
    \n
  • Set the header Content-Type: text/event-stream.
  • \n
  • Disable caching (Cache-Control: no-cache).
  • \n
  • Keep the connection open.
  • \n
  • Send data in the format data: ...\\n\\n.
  • \n
\n\n

Example 1: Simple SSE Server in Java Servlet

\n\n

Let's create a servlet that sends the current time every 5 seconds.

\n\n
import javax.servlet.ServletException;\nimport javax.servlet.annotation.WebServlet;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport java.io.IOException;\nimport java.io.PrintWriter;\nimport java.time.LocalTime;\nimport java.util.concurrent.Executors;\nimport java.util.concurrent.ScheduledExecutorServ                

Blogs

Book Recommendations