First steps with GWT and Errai messaging

First steps with GWT and Errai messaging

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.

Now we can integrate a basic Errai Messaging. In the reference you can see snippets of how to implement sending/receiving messages between client and server.

GWT projects typically differentiate client and server code in form of sub-packages.

Source: http://developerlife.com/tutorials/?p=124

In 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 RequestDispatcher and MessageBus.

In addClickHandler method we send a message to our MessageController.

In onModuleLoad method we subscribe message from server with MessageBus.

We also have to import ErraiBus to our GWT module xml configuration file as another module.

and add ErraiServlet to our web.xml

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 context-param in web.xml

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 RequestDispatcher and MessageBuilder in 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: null

and 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 MessageBus on server-side. You can create MessageBus on server-side using:

Now MessageController class looks like this:

and the server can send messages to the client without the client’s initiation.