title: Netty - How to get server response in the client
body: <p>I'm mostly there with Netty but one concept is still alluding me, and I can't find anything in the tutorials and so on. Firstly I do understand that Netty is asynchronous, but there must be a way for a client to call the server and be able to get a response beyond the handler. Let me explain more.</p>
  
  <p>I have a client as illustrated below. And please note that I understand it's bootstrapped and a new connection is established on each call, that's just there to make the example smaller and more succinct. Please ignore that fact.</p>
  
  <p>Client.java</p>
  
  <pre><code>// ServerResponse is a result from the server, in this case 
  // a list of users of the system (ignore that each time it's all bootstrapped).
  
  public User[] callServerForInformationFromGUIWidget()
  {
      ClientBootstrap bootstrap = new ClientBootstrap(...);
      bootstrap.setPipelineFactory(...);
  
      ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
      Channel channel = future.awaitUninterruptibly().getChannel();
  
      // Where request is a POJO sent to the server, 
      // with a request such as get me a list of users
      RequestPojo request = new RequestPojo(requestUserListCommand);
  
      ChannelFuture lastWriteFuture = channel.write(request);
  
      if(lastWriteFuture != null)
          lastWriteFuture.awaitUninterruptibly();
  }
  </code></pre>
  
  <p>Now I understand how to get the data on the server, and fire back the result. The only thing is how do I handle it on the client side? Yes the clientHandler class can do something like the following:</p>
  
  <p>ClientHandler.java</p>
  
  <pre><code>@Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) 
  {
      User[] users = (User[])e.getMessage();
  }
  </code></pre>
  
  <p>The problem is how does the client code actually get that result? All the examples are similar to a chat service, where the event fires off something else on the client that's not waiting on a response. Even the http client example I found lacking this. The documentation overall is really good, but it's lacking on how to do callbacks. Anyways, in this case I need the client to get the response from the server, and based on the results it will do what it needs. </p>
  
  <p>In other words, how do I write the client to do something like this:</p>
  
  <p>IdealClient.java</p>
  
  <pre><code>// ServerResponse is a result from the server, in this case 
  // a list of users of the system.
  
  public User[] callServerForInformationFromGUIWidget()
  {
      ...
      RequestPojo request = new RequestPojo(requestUserListCommand);
      ChannelFuture lastWriteFuture = channel.write(request);
  
      if(lastWriteFuture != null)
          lastWriteFuture.awaitUninterruptibly();
  
      User[] users = resultFromCallToServer();
  
      performSomeAction(users);
  }
  </code></pre>
  
  <p>Because the handler doesn't know who is looking for the answer, or who asked the question. And if it's done in the handler, than how? </p>
  
  <p>Back to my comments about the examples, the http client (and handler) examples just dump the result to System.out. If you had a GUI how would you pass the result from your request up to the GUI? I never saw any examples for this.</p>