• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Server Sent Events Not working correctly  RSS feed

aparna bhogu
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code structure :

Server : Java Servlet
Client : Simple JSP
Communication : Server Sent Events every 1 second
Here is the problem.

My code needed the server to send updates every one second to the client as stated above. Hence, I added a while loop with a sleep of 1000 milliseconds in the servlet code as shown below. The following strange behavior is observed:

- While the server is sending updates to the client, and the client window closes by mistake, the server does not stop sending updates It continues sending the data.
- When the client is re-opened, it sends data much faster (almost double). For example, the server sends 60 seconds worth of updates (60 updates) in just 25-30 seconds. The server sends faster updates not only for this round of updates, but also for any subsequent updates.

On the other hand, when the servlet code is the default Server Sent Event code where server sends updates every 3 seconds and no while loop is involved, the server behaves as expected. Which is ::

- When the client window is closed while server is still sending updates, server stops sending the updates. The server resumes sending updates upon the client window being re-opened.
- Once, the window is re-opened, the rate which the data is being updated to the client is uniform : 3 seconds for a single update and 180 seconds for 60 updates.
The default Server Sent Events code for java where the update interval is 3 seconds is as shown below:

This server behavior is much unexpected. Am I writing the server side code wrong? I have looked around a lot and only found while loop method for modifying the server update interval. Is there any other method which I am missing? How can I overcome the above problem?

Thank you for your help in advance.

mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong in your code. few things is there any mechanism to identify the client at the server end? that identifies the client and resume the data sending like session tracking? or identifying the client?
as you mentioned i haven't run into any problems with speed up thing.
here is server

here is my client

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!