title: How to offload blocking operation to a worker Verticle using websockets and async request
body: <p>I implementing websockets using Vert.x 3.</p>
  
  <p>The scenario is simple: opening socket from client doing some 'blocking' work at the vertex verticle worker and when finish response with the answer to the client(via the open socket)</p>
  
  <p>Please tell me if I am doing it right:</p>
  
  <p>Created VertxWebsocketServerVerticle. as soon as the websocket is opening and request coming from the client I am using eventBus and passing the message to </p>
  
  <p>EventBusReceiverVerticle. there I am doing blocking operation.</p>
  
  <p>how I am actually sending back the response back to VertxWebsocketServerVerticle and sending it back to the client?</p>
  
  <p>code:</p>
  
  <p>Main class:</p>
  
  <pre><code> public static void main(String[] args) throws InterruptedException {
          Vertx vertx = Vertx.vertx();
         vertx.deployVerticle(new EventBusReceiverVerticle("R1"),new DeploymentOptions().setWorker(true));
          vertx.deployVerticle(new VertxWebsocketServerVerticle());
  }
  </code></pre>
  
  <p>VertxWebsocketServerVerticle:</p>
  
  <pre><code>public class VertxWebsocketServerVerticle extends AbstractVerticle {
  
  
      public void start() {
          vertx.createHttpServer().websocketHandler(webSocketHandler -&gt; {
  
              System.out.println("Connected!");
              Buffer buff = Buffer.buffer().appendInt(12).appendString("foo");
              webSocketHandler.writeFinalBinaryFrame(buff);
              webSocketHandler.handler(buffer -&gt; {
                  String inputString = buffer.getString(0, buffer.length());
                  System.out.println("inputString=" + inputString);
                  vertx.executeBlocking(future -&gt; {
                      vertx.eventBus().send("anAddress", inputString, event -&gt; System.out.printf("got back from reply"));
                      future.complete();
                  }, res -&gt; {
                      if (res.succeeded()) {
                          webSocketHandler.writeFinalTextFrame("output=" + inputString + "_result");
                      }
                  });
  
              });
          }).listen(8080);
      }
  
  
      @Override
      public void stop() throws Exception {
          super.stop();
      }
  }
  </code></pre>
  
  <p>EventBusReceiverVerticle :</p>
  
  <pre><code>public class EventBusReceiverVerticle extends AbstractVerticle {
  
          private String name = null;
  
          public EventBusReceiverVerticle(String name) {
              this.name = name;
          }
  
          public void start(Future&lt;Void&gt; startFuture) {
              vertx.eventBus().consumer("anAddress", message -&gt; {
                  System.out.println(this.name +
                          " received message: " +
                          message.body());
                  try {
                      //doing some looong work..
                      Thread.sleep(10000);
                      System.out.printf("finished waiting\n");
                      startFuture.complete();
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              });
          }
      }
  </code></pre>
  
  <p>I always get:</p>
  
  <pre><code>WARNING: Message reply handler timed out as no reply was received - it will be removed
  </code></pre>
  
  <p>github project at: <a href="https://github.com/IdanFridman/VertxAndWebSockets" rel="nofollow">https://github.com/IdanFridman/VertxAndWebSockets</a>
  thank you,
  ray.</p>