Push notification for Java web app

34,969

Solution 1

If you can upgrade to or are using JDK 7 I suggest using Vert.x Vertx.io , use Sockjs on the client side. Vert.x has a complete sockjs server implementation, I ll try to suggest a way to implement this, for the rest please look at the Docs

The server implementation could be like this

    Vertx vertx = Vertx.newVertx();
    EventBus eventBus = vertx.eventBus()
    HttpServer server = vertx.createHttpServer();
    JsonArray permitted = new JsonArray();
    permitted.add(new JsonObject());
    SockJSServer sockJSServer = new DefaultSockJSServer(vertx, server);
    sockJSServer.bridge(new JsonObject().putString("prefix", "/pusher"), permitted, permitted);
    server.listen(<some port>);

On the client side register a handler like so on document load

 function () {
if (!eb) {
  eb = new vertx.EventBus("http://<your_host>:<your_port>/pusher");

  eb.onopen = function() {
   console.log("connected")
  };

  eb.onclose = function() {
    console.log("Not connected");
    eb = null;
  };
}

}

You can then register a handler to any address - address here can be anything , assume it is "AwesomeNotifications"

function subscribe(address) {
if (eb) {
  eb.registerHandler(address, function(msg, replyTo) {
  console.log("Reply recieved")
          });

}
}

Once you have this all set up , you can now push any data from the server to this address using the event bus we created earlier

eventBus.publish("AwesomeNotifications", new JsonObject(<some hashmap to serialize>))

Hope this helps

Solution 2

You can use HTMl5 server-send option. Here you can get more details

Server-Send option :

http://www.w3schools.com/html/html5_serversentevents.asp

Java servelt for server-send :

Java servlet and server sent events

Tutorial :

http://peaktechie.blogspot.in/2012/04/small-tutorial-on-html5-server-sent.html

HTML5 supported browsers :

http://fmbip.com/litmus

Solution 3

I've been looking into this recently. An event based approach is a great wasy to structure single page web apps, but can be used in multipage webapps too.

There are a number of ways to do this:

  • ActiveMQ AJAX - publish and subscribe to JMS events directly from javascript in the browser. This is quite basic, with less bells and whistles than the other approaches, but because of it's simplicity, might be a good base to start with if you a) already use activemq, b) like not having too many layers of abstraction
  • Atmosphere - Event based framework, can auto detected the best communication mechanism based on which webserver and which broswer are currently in use. A pretty nice framework, which supports a full spread of browsers and web severs, even down to IE6. And there are examples of using Atmosphere with spring MVC and Spring Integration.
  • Cometd - An implementation of the Bayeux protocol (to auto-negotiate the best connection type) based on jetty/hightide. Jetty was the first java webserver to support continuations, now part of the latest Servlet spec. Cometd take Jetty and wraps it up with JS client libraries for autodetection of the best connection mechanism to the browser.
  • Vert.x - An event based server platform that you can build on top of. There has been some controversy around Vert.x recently when it's author left VMware, but VMware retained the project. It now looks like version 2 will be released from the Eclipse Foundation. Seems very promising, but quite low level. Not the kind of thing you just plug into an existing java web app.
  • HTML5 EventSource - Standards based way of sending events to the browser. No mechanism for sending events back to the server. It's interesting, but given you need to implment a fallback for IE6, IE7 and IE8, it might not be your best choice, for now.

Ulimately, I think for the author the of question, integrating to a project, Atmosphere is clearly the best fit.

Edit: typos

Solution 4

I used Atmosphere to do this. It uses WebSockets if they are supported by the browser, and falls back nicely to polling when not. They also have a nice jQuery plugin to make life simple.

Solution 5

I'd suggest you go with Long Polling aka Comet. I've used it to throw random numbers to the client. You can also refer to this question.

Share:
34,969
Tarun Gupta
Author by

Tarun Gupta

Java Developer

Updated on July 05, 2022

Comments

  • Tarun Gupta
    Tarun Gupta almost 2 years

    Currently I am working on a web app which uses Spring 3.1 and Hibernate 4.

    As per the requirement, I want to implement push notifications like Facebook does, on a JSP page. If you have any suggestions, then please also list the compatible browsers with their versions.