Search code examples
angularvert.xvertx-eventbus

Java Vertx Eventbus Bridge with Angular on client side


I have implemented vertx eventbus bridge on my client side(Angular) and it works to some extend. I mean some time I get the send message from my Java Vertx application on the client side but some times I get this below error. Can someone please help me to debug or tell me why I get this error. I have a feeling for some reason the client is not registering.

> ERROR Error: INVALID_STATE_ERR
>     at EventBus.registerHandler (vertx-eventbus.js:279:13)
>     at SafeSubscriber._next (core.service.ts:83:16)
>     at SafeSubscriber.__tryOrUnsub (Subscriber.ts:265:10)
>     at SafeSubscriber.next (Subscriber.ts:207:14)
>     at Subscriber._next (Subscriber.ts:139:22)
>     at Subscriber.next (Subscriber.ts:99:12)
>     at FilterSubscriber._next (UnsubscriptionError.ts:101:24)
>     at FilterSubscriber.next (Subscriber.ts:99:12)
>     at BehaviorSubject._subscribe (BehaviorSubject.ts:27:18)
>     at BehaviorSubject._trySubscribe (Observable.ts:238:19)

and the code in the error is point to line 83 in my Angular code i.e. eventBus.registerHandler.

hostJobProgressSession(){
    console.log('Inside hostJobProgressSession!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
    console.log(this.eventBusService.getEventBus());
    this.eventBusService.getEventBus().subscribe(eventBus => {
      eventBus.registerHandler('update.job.onclient', (error, message) => {
        console.log('Inside registerHandler() -- received a message from server !! : ' + JSON.stringify(message.body));
        this.progressValue.next(message.body);

        if(error)
        {
          console.log('ERROR'+error)
            
      });
    })
  }

I will appreciate your help!!


Solution

  • Dont know if it makes a difference but ideally you should register the handler on eb.onopen, as follows:

    import EventBus from 'vertx3-eventbus-client';
    
    const eb = new EventBus("http://localhost:8080/eventbus");
    
    eb.onopen = () => {
      eb.registerHandler('update.job.onclient', function(error, message) {
        console.log('received a message: ' + JSON.stringify(message));
      });
    }
    eb.onclose = (param) => {
      console.log('closed', param)
    }
    
    export default eb;
    

    on the server side, you will need BridgeOptions. SockJSHandler (and likely CORS handler)

    ...
    Router router = Router.router(vertx);
    
    BridgeOptions opts = new BridgeOptions().addOutboundPermitted(
          new PermittedOptions().setAddress("update.job.onclient"));
    
    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);
    ...
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);