Sending events from the client to the server using RPC

server component. The server component can then take appropriate action - e.g updating the shared state or calling event listeners.

To set up client-server RPC we need to create one interface defining the RPC methods, and then make use of that interface on both the client and the server. Place the MyComponentServerRpc interface in the client package:

Java

  1. package com.example.mycomponent.client;
  2. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  3. import com.vaadin.terminal.gwt.client.communication.ServerRpc;
  4. public interface MyComponentServerRpc extends ServerRpc {
  5. public void clicked(MouseEventDetails mouseDetails);
  6. }

Note that the RPC methods can not have return values. In this example, we pass MouseEventDetails to get a more complete example, but you could pass almost any (or no) parameters.

In the server side MyComponent we need to implement the interface, and register it for use:

Java

  1. package com.example.mycomponent;
  2. import com.example.mycomponent.client.MyComponentServerRpc;
  3. import com.example.mycomponent.client.MyComponentState;
  4. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  5. import com.vaadin.ui.AbstractComponent;
  6. public class MyComponent extends AbstractComponent {
  7. private int clickCount = 0;
  8. private MyComponentServerRpc rpc = new MyComponentServerRpc() {
  9. public void clicked(MouseEventDetails mouseDetails) {
  10. clickCount++;
  11. setText("You have clicked " + clickCount + " times");
  12. }
  13. };
  14. public MyComponent() {
  15. registerRpc(rpc);
  16. }
  17. /* Previous code commented out for clarity:
  18. @Override
  19. public MyComponentState getState() {
  20. return (MyComponentState) super.getState();
  21. }
  22. public void setText(String text) {
  23. getState().text = text;
  24. }
  25. public String getText() {
  26. return getState().text;
  27. }
  28. */
  29. }

Here we react to the RPC call by incrementing a counter. We do not make use of the MouseEventDetails (yet). Notice the important call to registerRpc() in the added constructor.

In the client side MyComponentConnector, we use RpcProxy to get an implementation of the RPC interface, and call the clicked() method when the widget is clicked:

Java

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

Notice that most of the code is for attaching the click handler and creating the MouseEventDetails, the code for the actual RPC is quite minimal.

Compile the widgetset, and the label text should be updated with the click count whenever you click it (remember that the counting is done on the server-side).

Finally, note that you can use multiple RPC interfaces in one component, allowing for better code separation and reuse.

You can do the same thing in the other direction, see the article about server to client RPC for details.