Push notification for Java web app [closed]

2019-01-30 20:15发布

问题:

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.

回答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



回答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



回答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 spring-mvc project, Atmosphere is clearly the best fit.

Edit: typos



回答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.



回答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.



回答6:

My suggestion would be to use either publish-subscribe based MQTT middleware or XMPP protocol. If you are planning to send small chunks of messages, then it will be most appropriate to use MQTT because it is the most lightweight way to go. It has lots of client libraries including javascript and very easy to develop and manage.



回答7:

We have used XMPP for a long time to support push notifications, but mantain an Ejabber server (or whatever) is very dificult.

Also, actualy we have moved to socket.io that has client and server implementation, but you must to mantain nodejs server and start to play with js.

I also have tested faye in one project and works perfectly for Ruby enviroments.

But at the end, I think that the better solution is to use a library that you can embed in your server (java) and forget other tecnologies, I think that Atmosphere is a good solution with client and server also.