Skip to main content

Server-Sent Events with Java back-end using Servlets

    Yesterday, I provided an example of how to connect a client-side JavaScript EventSource object to a server-side Java Servlet for the use of server-sent events and, in particular, notifications. However, the example that I provided, like almost all similar examples on the Internet, was rudimentary. It lacked the ability to be efficient for real world applications. This was fine for the time being, because it was only created to emphasize and explain how to design such a system. But it led me to think how I could take that example and make it a little more applicable. So, after some thinking, and a little difficulty, I came up with a more efficient implementation.

    While developing this solution, I came to find out that there seems to be some problems with the EventSource Interface and how browsers implement it. This isn't too hard to believe since the API is still relatively new. However, it is slightly a nuisance to work with since some behavior is out of your hands. Also, it took awhile to think of a way to notify the Servlet that an event had occurred. This is due to the fact of how Servlets work. I ended up having to Inject an Application Scoped Stateful EJB into the Servlet that would observe events from a custom Event object. In my implementation, I didn't provide a way to fire an event or to create and retrieve a user Id. I just focused on the notification aspect. The Gist can be found here.

    Hopefully, this will provide a little more insight into how to develop a real world applicable notification system using a Servlet and EventSource object. Note, I haven't thoroughly tested the code, so if you find any errors or have another way to implement it, please let me know.


  1. Excellent post!!! Java is most popular and efficient programming language available in the market today. It helps developers to create stunning desktop/web applications loaded with stunning functionalities. JAVA J2EE Training in Chennai | Best JAVA Training institute in Chennai

  2. The expansion of internet and intelligence in business process lead the way to huge volume of data. It is important to maintain and process these data to be efficient in data handling. Hadoop Training in Chennai | Big Data Training in Chennai

  3. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing...
    ccna training in Chennai|ccna courses in Chennai|ccna training center in Chennai


Post a Comment

Popular posts from this blog

Face detection and live filters

Live video filters are becoming a popular trend fueled by Facebook (through their purchase of Msqrd) and Snapchat incorporating the features into their apps. These filters apply images or animations to your face using face tracking software. This technology has been around for awhile but is becoming increasingly more common due to the powerful CPU's that our mobile phones now have. Google provides an API that provides face tracking abilities through the Google Play Services library called Mobile Vision. I'm going to use their API to build a basic live filter app. The end result will look something like this:

    The bounding box wraps around the detected face and the sunglasses are the filter I chose (which is just a PNG image) which are drawn over the eyes. You could use any PNG image (with alpha for the background) you want, you will just have to adjust the layout according to where the image should be displayed. As you move your head, the box and sunglasses are redrawn…

Setting Up Connection Pooling With Elastic Beanstalk

Amazon's Elastic Beanstalk is a service which automatically scales your application when needed. It uses Amazon's Elastic Compute Cloud (EC2) instances as deployable containers which when your app requires more resources more containers will be deployed. This removes the need to manually configure your EC2 instance whenever you need more connections or resources and attempts to add simplicity to the maintenance aspect of your application. So, when you get more users of your app, your app will scale accordingly.

    Unfortunately, along with the ability to scale automatically, comes less control and configuration. Things you would normally have the ability to configure to your liking, such as your server, you no longer can. Amazon attempts to address this issue with configuration files. You can provide configuration files which can set up your server. These files are either written in JSON or the horrible format YAML. Though these files provide some level of control, you ca…

Android Guitar Tuner

Recently I created a guitar tuner application for Android that is written with pure Java (no C++ or NDK usage). The design was inspired by the Google Chrome team's guitar tuner web app using the WebAudio API. I wanted to code a version written natively for Android that didn't have to rely on a WebView, the WebAudio APIs, or server-side logic. Also, I wanted this application to be available to as many versions of Android as possible (whereas the WebAudio API may only be supported in more recent versions of WebView available only on newer flavors of Android). So, I coded it from scratch. I used a portion of the open source TarsosDSP project (their YIN algorithm) to help with the pitch detection.

    The application is available in the Google Play Store for Android: The project is completely open source and the code can be found on the GitHub repository: Fi…