Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notification for Java web app [closed]

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.

like image 998
Tarun Gupta Avatar asked Jan 23 '13 07:01

Tarun Gupta


2 Answers

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

like image 139
winash Avatar answered Oct 01 '22 13:10

winash


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

like image 34
SANN3 Avatar answered Oct 01 '22 14:10

SANN3