Using RPC to send events to the client

An RPC mechanism can be used to communicate from the server to the client. In effect, the server-side component can call methods that are executed by the client-side connector. As opposed to shared state (discussed in a separate article), no information is automatically re-transmitted when the client-side state is lost (e.g when a browser reload is invoked).

Whether shared state or RPC is appropriate depends on the nature of the data being transmitted, but if the information transmitted needs to be retained on the client over a page refresh, you should probably use shared state. You’ll probably find shared state more appropriate in most cases, and server-client RPC extremely useful in a few cases.

To set up server-client RPC, we need to create an interface extending ClientRpc for the RPC methods, then register an implementation of the RPC interface in the client-side connector, and call the method(s) via a proxy on the server. This is the reverse of the server-client RPC described in a separate article.

We’ll create MyComponentClientRpc in the client package:

Java

  1. package com.example.mycomponent.client;
  2. import com.vaadin.shared.communication.ClientRpc;
  3. public interface MyComponentClientRpc extends ClientRpc {
  4. public void alert(String message);
  5. }

Again, note that the RPC methods can not return anything, but can take multiple arguments.

In MyComponentConnector we register the RPC implementation in the constructor. This time we’ll create the implementation inline:

Java

  1. package com.example.mycomponent.client;
  2. // imports removed for clarity
  3. @Connect(MyComponent.class)
  4. public class MyComponentConnector extends AbstractComponentConnector {
  5. MyComponentServerRpc rpc = RpcProxy
  6. .create(MyComponentServerRpc.class, this);
  7. public MyComponentConnector() {
  8. registerRpc(MyComponentClientRpc.class, new MyComponentClientRpc() {
  9. public void alert(String message) {
  10. Window.alert(message);
  11. }
  12. });
  13. /* The rest of the code remains unchanged:
  14. getWidget().addClickHandler(new ClickHandler() {
  15. public void onClick(ClickEvent event) {
  16. final MouseEventDetails mouseDetails = MouseEventDetailsBuilder
  17. .buildMouseEventDetails(event.getNativeEvent(),
  18. getWidget().getElement());
  19. rpc.clicked(mouseDetails);
  20. }
  21. });
  22. }
  23. @Override
  24. protected Widget createWidget() {
  25. return GWT.create(MyComponentWidget.class);
  26. }
  27. @Override
  28. public MyComponentWidget getWidget() {
  29. return (MyComponentWidget) super.getWidget();
  30. }
  31. @Override
  32. public MyComponentState getState() {
  33. return (MyComponentState) super.getState();
  34. }
  35. @OnStateChange("text")
  36. void updateText() {
  37. getWidget().setText(getState().text);
  38. }
  39. */
  40. }

(MyComponentServerRpc is introduced in Sending events from the client to the server using RPC. Window here is com.google.gwt.user.client.Window, not com.vaadin.ui.Window.)

Finally, in MyComponent we use the RPC via a proxy:

Java

  1. import com.vaadin.ui.AbstractComponent;
  2. public class MyComponent extends AbstractComponent {
  3. private int clickCount = 0;
  4. private MyComponentServerRpc rpc = new MyComponentServerRpc() {
  5. public void clicked(MouseEventDetails mouseDetails) {
  6. clickCount++;
  7. // nag every 5:th click
  8. if (clickCount % 5 == 0) {
  9. getRpcProxy(MyComponentClientRpc.class).alert(
  10. "Ok, that's enough!");
  11. }
  12. // setText("You have clicked " + clickCount + " times");
  13. }
  14. };
  15. /* Unchanged code follows:
  16. public MyComponent() {
  17. registerRpc(rpc);
  18. }
  19. @Override
  20. public MyComponentState getState() {
  21. return (MyComponentState) super.getState();
  22. }
  23. public void setText(String text) {
  24. getState().text = text;
  25. }
  26. public String getText() {
  27. return getState().text;
  28. }
  29. */
  30. }

That is: every fifth time the label is clicked, we get the RPC proxy by calling getRpcProxy() and call our alert() method with a message to send to the client.

Compile the widgetset, and you’re all set to try out server-client RPC.