How to setup a simple SockJs between Vert.x and Angular client

99 views
Skip to first unread message

Zolf

unread,
May 24, 2022, 3:39:31 AM5/24/22
to vert.x
Hello everybody,

I am trying to implement a very simple case where I can send a simple string to my Angular client from my Vertx backend.  This is my first experience with it, so please bear with me.

To get started I did something on these bases.
//Server side Vert.x app
//////Sockjs

        SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
       
        SockJSBridgeOptions options = new SockJSBridgeOptions()
          .addInboundPermitted(new PermittedOptions().setAddress("/client.register"))
          .addOutboundPermitted(new PermittedOptions().setAddress("service.ui-message"));
       
       
        Router subRouter = sockJSHandler.bridge(options, event -> {
            System.out.println("A websocket event occurred: " + event.type() + "; " + event.getRawMessage());
            LOG.error("INSODE subRouter!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+event.getRawMessage());
            LOG.error("event.type()!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+event.type());
          if (event.type() == BridgeEventType.SOCKET_CREATED) {
              LOG.error("A socket was created!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
          }
       
          // This signals that it's ok to process the event
          event.complete(true);    
        });
       
        router.mountSubRouter("/eventbus", subRouter);
       
        vertx.eventBus().publish("service.ui-message", "HELLO FROM ME");
       
        addCorsMappings(subRouter);
       
        //////

and on the Angular client side I have this code

getPrintJobProgress()
  {
    var eb = new EventBus('http://localhost:1234/eventbus/');
    console.log("ebbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: ", eb);

    eb.onopen = () => {
      // set a handler to receive a message
      eb.registerHandler('service.ui-message', (error, message) => {
        console.log('received a message: ' + JSON.stringify(message));
      });

      // send a message
      eb.send('service.ui-message', {name: 'tim', age: 587});
     }
  }

But nothing is happening in my server and client side.
I also used Postman to see when happens when I go to the URL localhost:1234/eventbus and I get response saying -- Welcome to SockJS!
I referred to the doc on the vertx site but cannot move forward. 
Thanks for any help

Zolf


Bruno F

unread,
May 27, 2022, 5:41:37 AM5/27/22
to vert.x
In case it may help, here how I setup my sockjs handler:

route("/eventbus").subRouter(
  SockJSHandler.create(vertx).bridge(
     SockJSBridgeOptions(new JsonObject())
       .addOutboundPermitted(new PermittedOptions().setAddressRegex("address"))
   )
)

Bruno.
Reply all
Reply to author
Forward
0 new messages