Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send messages from server to client?

I am going to implement something similar to Facebook notification and this website (StackOverflow's notification which is notifying us if anyone write a comment/answer etc for our question). Please note users are going to use my application as a website not mobile application.

I came across following answer which fetch the results, but I need to push the results not fetch.

Based on suggestions I have created a simple method in my entity class and added the @PostPersist to it but it has not worked so based on this answer I added the persistence.xml file to define the listeners but after session.save(user) the aftersave method does not get triggered.

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>
like image 686
J888 Avatar asked Jun 27 '13 07:06

J888


1 Answers

Sounds like a task for WebSockets. It is part of Java EE 7 so the Glassfish should be one of the first AS that will support it.

For intercepting the DB access you can use @PostUpdate and @PostPersist. Here is related question.

There are many ways to do the so called Server Push for notifying the connected clients:

  • polling (the link you've provided in the question ("Are we there yet? Are we there yet? ..."))
  • long polling (smarter way of polling - long-lived HTTP technique using keepalive messages)
  • WebSockets (JSR 356)
  • piggy-backing
  • SPDY(wiki)
  • Server-Sent Events (related answer, wiki)

EDIT: In the Java world, there are couple of frameworks where server push (reverse ajax) is implemented out-of-the box. If you are familiar with GWT, I would suggest Errai. Other alternative is the Atmospere. The downside of the Atmospere is the fact that it requires standalone running process next to your regular application server with your web app. I was playing with it a year ago so this may have been changed since then.

In general, it is hard to provide you with a concrete piece of code, because it depends on the framework you will choose. I am familiar with Errai so here is an example in it:

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 benefits of using the Errai is having the CDI on the server and on the client out-of-the-box, another thing that is nice is using the web-sockets under the covers if it is supported and falling back to other solutions otherwise.

Whatever you choose, it should fit to your existing infrastructure and to your client side UI framework.

like image 110
Jiri Kremser Avatar answered Sep 18 '22 23:09

Jiri Kremser