Martin Nielsen
Martin Nielsen

Reputation: 2049

Re-render wicket component from backend

I am working with a wicket page where i need to allow a backend process to instruct a component on a page to refresh. I don't want to refresh the entire page, just a single component, so i am guessing that i'm in Ajax territory.

I have found a few different Ajax timers like AjaxSelfUpdatingTimerBehaviour but i really would like an actual push, as the changes should be fairly instantaneous and i would require some high-frequency timers.

Does anyone know of a good way to achieve this?


Edit 1

I have been looking at the Wicket Websocket implementation. I can see the WebSocket behaviours looks promising, but the only server-push method i can find is something like this:

IWebSocketConnectionRegistry registry = new SimpleWebSocketConnectionRegistry();
  Application application = Application.get(applicationName);
  IWebSocketConnection wsConnection = registry.getConnection(application, sessionId, pageId);

  if (wsConnection != null && wsConnection.isOpen()) {
    wsConnection.send("Asynchronous message");
  }

But i don't see any way to update a component in a typesafe way like the Ajax Behaviours like target.add(component). Can someone elaborate or point me in the right direction? Google is not being my friend.

Upvotes: 2

Views: 628

Answers (2)

papkass
papkass

Reputation: 1289

I'm using WebSocketPushBroadcaster to push objects.

IWebSocketSettings webSocketSettings = IWebSocketSettings.Holder.get(this);
WebSocketPushBroadcaster broadcaster = new WebSocketPushBroadcaster(webSocketSettings.getConnectionRegistry());
Application application = Application.get(applicationName);
broadcaster.broadcastAll(application, myObj);

Check for events on the page where you need the object:

@Override
public void onEvent(IEvent<?> event)
{
  if (event.getPayload() instanceof WebSocketPushPayload)
  {
     WebSocketPushPayload wsEvent = (WebSocketPushPayload) event.getPayload();
     if (wsEvent.getMessage() instanceof MyObj)
     {
        MyObj myObj = wsEvent.getMessage();
        // do stuff
        wsEvent.getHandler().add(myComponent);
     }
  }
}

I'm using these dependecies:

    <dependency>
        <groupId>org.apache.wicket</groupId>
        <artifactId>wicket-native-websocket-core</artifactId>
        <version>6.21.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.wicket</groupId>
        <artifactId>wicket-native-websocket-jetty</artifactId>
        <version>6.21.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.wicket</groupId>
        <artifactId>wicket-native-websocket-tomcat</artifactId>
        <version>6.21.0</version>
    </dependency>

Upvotes: 1

svenmeier
svenmeier

Reputation: 5681

You should look into websockets:

https://ci.apache.org/projects/wicket/guide/7.x/guide/nativewebsockets.html

Upvotes: 1

Related Questions