Widget RPC
A new generation client-server communication for Vaadin widgets.
Traditionally Vaadin widgets use simple paint-change cycle to synchronize the client and server parts of a UI component.
Widget RPC introduces a new way of communicating using method calls in more RPC-like manner and enables developers to build more fine-grained communication more easily.
First, register a listeners:
rpc.register("doStuffWithThings", new Method() { void invoke(String methodName, Object[] params) // do stuff with things } });
Then you can invoke methods like this:
rpc.call("doStuffWithThings", aThing, anotherThing);
This works both ways (client to server and server to client). See the highlights and documentation for details.
....
Even this mechanism is in use in several of my components like the Console, SWFObject and YouTubePlayer, I consider it as "experimental" until I get some positive feedback and the users are comfortable with the API.
NOTE: For Vaadin 7 has its own very similar mechanism and this component is not needed.
Sample code
@ClientWidget(VMyComponent) MyComponent extends AbstractComponent implements ServerSideHandler { /* Create a server-side call proxy */ private final ServerSideProxy client = new ServerSideProxy(this); /* Register the callbacks to receive calls from the client */ public MyComponent() { client.register("input", new Method() { public void invoke(String methodName, Object[] params) { handleInput((String) params[0]); } }); } /* Basic component API method that simply calls the client-side implementation */ public void print(String string) { client.call("print", string); } /* Forward the paint request to Vaadin's change track. */ public void requestRepaint() { this.requestRepaint(); } /* Send initialization data to the client-side */ public Object[] initRequestFromClient() { return new Object[] { 80, 25, true, "Welcome to console demo", "}> " }; } /* Default handler for calls from the client-side. */ public void callFromClient(String method, Object[] params) { // Handle (otherwise unhandled) calls here } /* Forward the paint to the call proxy handler. */ @Override public void paintContent(final PaintTarget target) throws PaintException { super.paintContent(target); client.paintContent(target); } /* Forward the variable changes to the call proxy handler. */ @Override public void changeVariables(final Object source, final Map<String, Object> variables) { super.changeVariables(source, variables); client.changeVariables(source, variables); } }
VMyComponent extends SimplePanel implements ClientSideHandler { /* Client-side method call proxy. */ private final ClientSideProxy rpc = new ClientSideProxy("VMyComponent", this); /* Register client-side method callbacks in the constructor */ public VMyComponent() { rpc.register("print", new Method() { public void invoke(final String methodName, final Object[] data) { print((String) data[0]); } }); } /* Client-side implementation of print method */ public void print(String string) { // TODO: implementation skipped } /* Call a server-side method (only if we are initialized already) */ public void onTextEntered(String string) { if (rpc.isClientInitialized()) { rpc.call("input", string); } } /* Let the method proxy to handle all communication */ public void updateFromUIDL(final UIDL serverData, final ApplicationConnection appConn) { rpc.update(serverData, appConn); } }
Links
- Author Homepage
- Online Demo
- Issue Tracker
- Discussion Forum
- Documentation
- Add-on: YoutubePlayer
- Add-on: SWFObject
- Add-on: Console
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
Fixed Vaadin's URI translation for resource parameters. (Thanks Sasha!)
- Released
- 2011-08-29
- Maturity
- EXPERIMENTAL
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 6.3+
- Browser
- Browser Independent