When I was new at my current job, I got a task to realize server push messages from server to client. The application used GWT and my supervisor recommended to me to use Errai framework. At that time I had no experience with either GWT or Errai.
More details about server push in GWT is described in more depth in this article. You can find some articles about messaging in GWT like GWT consumer + Server push tutorial and use GWTEventService or GWT Comet Adapter for StreamHub. Maybe these solutions don’t fit to your requirements, some of these projects may not be in active development and probably wouldn’t work with new versions of GWT. At the end of this article, I will demonstrate a server push in Errai (the code is viewable on GitHub with a working example).
At first I created a basic GWT project. Lars Vogel wrote great tutorial on how to develop a basic web application with GWT. In this simple application you have a button with click action handler and when you click on this button, “user data” are created on server-side and rendered in table on client side. I used this project to demonstrate Errai messaging in GWT. For building whole project I used Maven and gwt-maven-plugin.
GWT projects typically differentiate client and server code in form of sub-packages.
|com/example/app||Root project package. This package contains all the module files. No code can be placed in the root project package.|
|com/example/app/client||Sub-package containing all the client-side source code.|
|com/example/app/server||Sub-package containing all the server-side source code|
server sub-package we create a
MessageController class which ensures receiving message from client and sending them back a reply.
Now in client service, which is EntryPoint we initialize
addClickHandler method we send a message to our
onModuleLoad method we subscribe message from server with
We also have to import
ErraiBus to our GWT module xml configuration file as another module.
ErraiServlet to our
Now when we click on the button, the client should send the message to the server, which generates a new message which the client shows in the alert window.
If you get an
org.jboss.errai.bus.client.api.base.NoSubscribersToDeliverTo exception, you probably forgot the ErraiApp.properties file. This property file is a marker for classpath scanning and can be usually empty. We can define here Dispatcher implementation, which is not necessary, because by default it uses SimpleDispatcher. In reference you can find more options for ErraiBus configuration.
and add this property file as a
Server Push in Errai
In our previous example the client can send the message to the server and the server can send message back to the client, but we are in situation where we want the server to send messages to the client without the client’s initiation.
At first I tried to remove the
ClientService class, and send the message on server side, when “user data” are created. With this approach, I got the following error:
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.util.List gwt.user.client.UserService.getUserList()' threw an unexpected exception: org.jboss.errai.bus.client.api.base.MessageDeliveryFailure: could not deliver message: nulland tried to figure out why it doesn’t work. I spent some time studying source code of errai-bus. The point was to send message with
MessageBuson server-side. You can create
MessageBuson server-side using:
MessageController class looks like this:
and the server can send messages to the client without the client’s initiation.