Flax engine: GWT event bus implementation

In any software system there is going to be events, such as onComponentLoad, onMouseClick etc. Flax as an engine was based off another engine I had written in C++ using SDL as the graphics API, though it was never really finished and lacked a key element from the start: the ability to create custom events, which objects could listen for. What resulted was code which passed information between objects solely as parameters which made the code coupled which is really something that should be avoided. So when it came to architecting Flax, custom events were to be key in my design.

Task Analysis

After looking at how custom events where implemented in Google Web Toolkit I messed around with some code and found a problem. For an object to listen for an event you must register that object with the event sender, that is, tell the sender of the event to add that object to the list of objects which the event will be sent to. For a small number of event senders and receivers this may work but not in the case of Flax, as there could be hundreds of objects in-game needing to receive and send events.


At the time I really didn’t know enough about how the events actually worked to come up with a solution to the problem. So of course I headed on over to Stack Overflow and a kind person pointed me in the direction of this talk: Google I/O 2009 – Best Practices for Architecting GWT Apps by Ray Ryan. Ray talks about how to implement a bus system for events in GWT, so that spaghetti architecture does not arise. I created the below image for the design document to illustrate how the Event Bus works. It’s quite simply really, instead of registering all objects with each other to send and receive, register each object with a globe bus which acts almost like a telephone operator routing the events to the objects that need to hear about them


GWT Event Bus

Setting up the bus

In the Flax engine, I set up the event bus very simply as a static class which allowed globe access to a handlerManager object.

 * This class has only one property and simply acts as a globe way of accessing the eventBus
 * @author Ciarán McCann
public class EventBus {

         * This is the events hanlder which is used for firing and registering events.
        public static HandlerManager bus = new HandlerManager(null);


Specific Event

Events are made up of 2 files: the event object itself which is e when passed as a parameter in the method onEvent(event e). The second is the event handler which is a simple interface which both the firing and listening objects must implement.

  1. [EventName]Event.java
  2. [EventName]EventHandler.java

After the event specific class, there are the classes which listen and fire the events, which require some small code modifications.

Firing Class

Static reference to the event bus is used to fire off an event.

Event.bus.fireEvent(new EVENT_CLASS());

Listening Class

In the constructor of the class there most be a static reference to the event bus which registers that class to listen for a certain event. The handler parameter for this would be “this”, which refers to the object itself.

Event.bus.addHandler(EVENT_CLASS.TYPE, this);

    public void onEvent(EVENT_CLASS e) {

        // e.stuff
       //do code on event



public interface onCollisionEventHandler extends EventHandler {
    void onCollision(onCollisionEvent e);


public class onCollisionEvent extends GwtEvent {

//Information about the event is stored in properties
//This event has currently no event information

public onCollisionEvent() {}

public static Type TYPE = new Type();

public Type getAssociatedType() {
return TYPE;

protected void dispatch(onCollisionEventHandler handler) {


The code above has been pulled from segments of the design document for the events system, in which I was using onCollision event as an example.
That’s how I implemented the GWT event bus architecture and it works perfectly for my needs, though again I am not an expert in GWT, I am merely distributing knowledge I have acquired from working with Google Web Toolkit. If you have any question or suggestion post them in the comments, thanks.


About the Author

Ciarán McCann

Flax Project Founder - Ciarán McCann is an extremely passionate and motivated programmer who has been programming for about 4 years now. Currently attending Carlow I.T studying computer games development. He has experience in many different programming languages, markup languages and general technologies. His role in the Flax Project is as a blogger/Web Designer and Flax Engine programmer. Please excuse any bad grammar/spelling, I am a bit on the Dyslexic side. Follow me on Twitter for info on what I am working on.

Visit Website


  1. Excellent way of explanation.Thanks much for the article.

Trackbacks for this post

  1. […] This post was mentioned on Twitter by Dr Carl Lange, Flax.ie. Flax.ie said: Flax engine: GWT event bus implementation http://t.co/n9TvErA […]