How to send messages from server to client?

I am going to implement something similar to a Facebook notification and this site (a StackOverflow notification that notifies us if someone writes a comment / answer, etc. for our question). Please note that users will use my application as a website, not a mobile application.

I came across the following answer that retrieves the results, but I need the results not to be retrieved.

Based on the suggestions, I created a simple method in the entity class and added @PostPersist to it, but it didn't work that way based on this answer . I added persistence.xml to define listeners, but after session.save (user) the aftersave method does not start.

User.java

@Entity public class User{ ..... @PostPersist public void aftersave(){ System.err.println("*****this is post persist method****"); } } 

persistence.xml

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- To change this template, choose Tools | Templates and open the template in the editor. --> <property name="hibernate.ejb.event.pre-insert" value="my.hibernate.events.listeners.Listener" /> <property name="hibernate.ejb.event.pre-update" value="my.hibernate.events.listeners.Listener" /> <property name="hibernate.ejb.event.pre-delete" value="my.hibernate.events.listeners.Listener" /> <property name="hibernate.ejb.event.post-insert" value="my.hibernate.events.listeners.Listener" /> <property name="hibernate.ejb.event.post-update" value="my.hibernate.events.listeners.Listener" /> <property name="hibernate.ejb.event.post-delete" value="my.hibernate.events.listeners.Listener" /> 

pom.xml

  <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.1.Final</version> <type>jar</type> </dependency> 
+8
java-ee mysql hibernate struts2 notifications
source share
6 answers

Sounds like a task for WebSockets . This is part of Java EE 7, so Glassfish should be one of the first ASs to support it.

You can use @PostUpdate and @PostPersist to intercept database @PostPersist . Here is the question link.

There are many ways to do the so-called server push to notify connected clients:

EDIT: There are several frameworks in the Java world in which server push (reverse ajax) is implemented out of the box. If you are familiar with GWT , I would suggest Errai . Another alternative is Atmospere . The drawback of Atmospere is the fact that it requires a standalone launch next to your regular application server with your web application. I played with him a year ago, so this may have been changed since then.

In general, it is difficult to provide you with a specific piece of code, because it depends on the structure you choose. I am familiar with Errai , here is an example:

Server side:

 @ApplicationScoped public class TickerService { @Inject private Event<Tick> tickEvent; private void sendTick() { tickEvent.fire(new Tick()); } } 

Client side:

 @EntryPoint public class TickerClient { public void tickHappened(@Observes Tick tick) { // update the UI with the new data } } 

Other advantages of using Errai are CDI on the server and on the finished client, another thing that is good is the use of web sockets under covers if it is supported and returns to other solutions otherwise.

No matter what you choose, it should match your existing infrastructure and client-side user interface.

+13
source share

mqtt can be used to redirect the server and send messages.

More information is available at http://mqtt.org/ .

========================================

Updated: July 11, 2013

Mqtt is a simple and easy messaging protocol for publishing / signing. If the server is the publisher and browser of the client, subscribe to the topic on which the server is published, and then the server can directly send a message to the client.

Some useful resources:

Mosquitto is an open source mqtt server. Easy to install and configure.

mqtt-client is a proven powerful mqtt java client.

+4
source share

Use Node JS and socket.io

This technology selects the best mode of transport based on the browser that the client uses.

For the latest browsers, it uses web sockets, while for others it competently degrades to Flash Socket or Long Pooling. See here for more details.

What you need to do is set up the server using these technologies. The server will run on a specific port. All clients will listen to this port, and the server will be able to transmit data to the client through this port.

+4
source share

Comet , also known as Reverse Ajax , is a web application model in which a long HTTP request allows the web server to send data to the browser without an explicit request from the browser.

Comet (a long HTTP server with AKA support) allows the server to respond very slowly to a browser request for information and continue to respond to a schedule dictated by the server. For more information about comets, see the following:

DWR is a Java library that allows Java on the server and JavaScript in the browser to interact and make calls to everyone as simple as possible. Using Reverse Ajax DWR allows Java code running on the server to use client-side APIs to publish updates for arbitrary groups of browsers. This allows you to interact in 2 ways - the browser, the calling server and the server browser. DWR supports Comet, Polling, and Piggyback (sending data with regular queries) as publishing methods in browsers.

DWR provides integration with Spring, Struts, Guice, Hibernate and others. You can read more here .

Other comets and AJAX backframes:

+4
source share

but after session.save (user), the aftersave method does not start.

  • @PostPersist is a JPA callback.
  • session.save() is a non-JPA sleeping proprietary method. JPA uses entityManager.persist() .
  • You are using incompatible features.
+2
source share

Check for updates from the server every 30 seconds or on demand.

 window.setInterval(function(){ /// call your function here //Make AJAX call //Update Respective HTML Contact i,e, DIV }, 30000); 
+2
source share

All Articles