1 Reply Latest reply on Jul 3, 2013 10:28 AM by Jonathan Fuerth

    Fundamental Question

    Mike Dennis Newbie

      Hey Jonathan and experts,


      Had  a fundamental question to understand whether a particular scenario can be implemented using ErraiBus.


      Firstly - Can one obtain an instance of the server erraibus using ErraiBus.get? I believe the documentation says that is only for client and the server side bus is managed by the container and IOC. What if I am not using IOC?


      Here is the scneario:


      - The client does an rpc call to the server that spawns a new thread to process a few things that could take a few mins.

      - Can this new thread (not instantiated through IOC - since it is an existing library) send a message on the bus so that the client can update the view


      If the above can be done, can you show an example?


      I have had no issues so far with conversation messages. For e.g. my HelloWorldService on the server that implements MessageCallback gets the message from the client and in its callback(Message message) method, it can reply or broadcast. This service is IOC and the bus is injected with @Inject.


      I am unclear how this would have to happen if the message needs to be initiated from the server - either in the above scenario or another scenario such as:


      - We have a polling system on a DB; when a certain record changes, we want to send a message from the server and let all the clients that are listening to update themselves.



      Can this be done or am I mistaken in the fundamentals of Errai?


      Thanks so much...



        • 1. Re: Fundamental Question
          Jonathan Fuerth Master

          Hi Mike,


          It's a good question, and one that I've tripped over in the past myself. The answer is no, ErraiBus.get() is only applicable in client code.


          Normally (as you've said already) you obtain an instance of the server MessageBus by means of @Inject on a field or constructor of your RPC or bus service class. Since both are created by the embedded Guice context in server-side Errai, the correct ServerMessageBusImpl instance is readily injectable from the context.


          In a case like yours, where you want to initiate a broadcast from a worker thread (outside the Guice injection context) you have little recourse but to fall back on a singleton reference. [Un]fortunately, there is a way:


              ErraiService service = ErraiServiceSingleton.getService();

              MessageBus bus = service.getBus();


          I'm not at all a fan of singletons like this; I can imagine a time when we may want several message buses operating independently (or perhaps in federation) within the same webapp, and the existence of an API like ErraiServiceSingleton.getService().getBus() is a big impediment to making that a reality. But for now, I think it's your best bet.