Eric Guan
Eric Guan

Reputation: 15982

GWT - Difference between (client --> server --> client) and (server --> client) communication

In my MVP app i have lots of (client --> server --> client) communication using GWT-RPC. The structure is straight forward, here's an example of a client call.

        testObject.callToServer(example, new AsyncCallback<Void>(){
            @Override
            public void onFailure(Throwable caught) {
                //handle failure
            }
            @Override
            public void onSuccess(Object result) 
                //handle success
            }
         });

My question is, how would i implement server to client communication only, without the initial call from the client? Can i still use RPC, and if so can i reuse the code example above somehow?

Some more information, i'm using WebSockets to maintain an open communication link between client/server. I'm trying to figure out how to send more than just strings over the wire. I recognize the fact that RPC and Websockets are 2 different kinds of communication, and they may be mutually exclusive within a single communication instance.

As for what kind of data I want to be sending, right now just simple POJOs.

Thanks.

Upvotes: 0

Views: 388

Answers (2)

Colin Alworth
Colin Alworth

Reputation: 18346

All methods of communicating with the server are 'just strings over the wire' - the difference with RPC is that it is a specially formatted string that describes the type, structure, and content of the objects being sent. The structures are predefined before the conversation even starts - in .class files for the server, and compiled into the JS format for the GWT code. There is a shared .gwt.rpc file in your app that then describes which types can be used in each pair of RPC interfaces, so that both sides can be sure that they know what the other side knows (specifically, the client names that file in each request, and the server agrees to use that file if it can be found, or else throw an error that the two are out of sync).

Putting objects into some other form of transport like websockets requires serializing objects into that string, and on the other side reading them out. To properly use RPC with the limitations it is designed to work with, you have to start the expected 'handshake', but since websockets starts from the client, this should be easily done.

In your self-answer, you mentioned that you switched to AutoBeans instead, letting you simply define very simple bean-like structures in interfaces, and be able to easily map them to JSON strings and back again. I've also done two simple implementations on the server of WebSockets plus RPC, with a single shared client impl: https://github.com/niloc132/webbit-gwt. This project support either JavaEE websockets, or Webbit (a websocket library that uses Netty). It isn't complete or bug-free, but lets you behave as if either side (server or client) can call the other freely, invoking methods with RPC-able objects, and provides some simple hooks for starting/stopping the socket.

Upvotes: 2

Eric Guan
Eric Guan

Reputation: 15982

I achieved this with GWT's Autobean framework.

Upvotes: 0

Related Questions