Errai: The browser as a platform

Tuesday, June 21, 2011

Pervasive CDI: Errai at the JBoss World Keynote


JBoss World kicked off with a real cool keynote presentation which represented a confluence of JBoss’s cutting-edge technology.

The demo brought together JBoss AS, Infinispan, HibernateOGM, CDI and Errai.

Of course, our little part of the presentation was the demonstration of Errai powering a real-time tag cloud which really brought home the concept of the "real-time web"; most certainly not something which hasn’t been done before, but it was how we did it that makes it interesting.

I'm not going to rehash too much of what has already been said in the aforementioned blog posts, and I highly recommend you click the hyperlinks and check them out. Instead, I'm going to talk about CDI and Errai.

CDI is a technology we’re pushing really aggressively at JBoss, and Errai is no exception. In fact, we’re relatively sure that we’re the one and only in-browser CDI engine.

It was only natural we’d want to show this off at JBoss World. And that we did.

The demo involved bringing in a live tweet stream from Twitter and then producing real-time information based on what hashtags were most popular. The key was to have a fully dynamic tag cloud which would be updated the instant that the information was available. And as such, we used Errai’s push technology to display the information to every browser simultaneously and in real time.

CDI in the Browser

So what does CDI in the browser look like? Well, this might come as a shock: it looks exactly as it does in your server-side code. Consider the following server code:

@ApplicationScoped
public class MyService {
    @Inject @Updated
    private <>Event updatedTagEvent; 

    ... 

}

And now consider the following client GWT code:

public void onTagUpdated(@Observes @Updated Tag tag) {
    System.out.println("Tag was updated: " + tag)
}

In the former code snipped we setup a CDI event consumer, and in the latter, we setup a CDI event observer. Believe it or not, that’s pretty much everything you need to know about push messaging with Errai CDI.

In fact, this is exactly how our tag cloud was updated. Using our transparent push mechanism, serialization and bus framework, all of the boilerplate associated with doing “push” is eliminated.

Yes, it’s that cool. I’m not going to show you a bunch of XML , property or boilerplate class files because, well, there are none.

As a developer you simply want to think about your logic and your data. Utilizing Errai and CDI to build your rich web applications allows you to do just that.

How does it work?

Errai is very much a compile-time framework. In order to provide the runtime infrastructure to transparently communicate between browser code (written in GWT) and the server, we provide a significant amount of code generation to support functionality based on declarative configuration.

For instance, we provide a straight forward annotation, @ExposeEntity which effectively instructs Errai to produce marshaller and demarshaller wrappers -- ultimately compiled down to JavaScript -- for the entity.

The other aspect of Errai is a powerful messaging bus which, consistently with Errai’s “pervasive” theme, runs concurrently in the browser and on the server. As opposed to thinking about client vs server, we have aggressively pursued a flat architecture where the client and server APIs are common and shared. So there isn’t one way to write server code for Errai and another way to write client code.

This also provides the underlying application with transport agnosticism. That is to say, this only contract is between the bus APIs and the client code for all client-server communication. Or even more high-level in the case of Errai CDI, between the application and the CDI eventing system. This means the method of communication, be it WebSockets or COMET is invisible to the application. Developers no longer need to worry about the particularities of working with those technologies.

The future?

We think the future of the web is rich applications. But not only that pervasive applications. And the demo we showed at JBoss World which can be viewed here at about 35 minutes into the presentation is merely a taste of how powerful pervasive computing can be.

3 comments:

  1. Has the code for the demo been posted somewhere?

    ReplyDelete
  2. The code can be found here: https://github.com/errai/JBWDemo

    ReplyDelete
  3. Can you tell me where I can find the persistenceService. It's not part of this project.

    I'm looking for an implementation of Errai with JPA/Hibernate. Can't find one yet. The current demo's contain all the ExposedEntity but not in combination with an JPA/Hibernate backend.

    ReplyDelete