Skip to content

Pushing javascript to the client

Lance edited this page Sep 20, 2013 · 15 revisions

Custom javascript can be pushed to the client when an event is received using the AjaxResponseRenderer

TML

<t:cometd.PushTarget topic="/myTopic" event="messageReceived" />

Java (page / component)

@Inject
private AjaxResponseRenderer ajaxResponseRenderer;

@OnEvent("messageReceived")
void onMessageReceived(final String message) {
    ajaxResponseRenderer.addCallback(new JavaScriptCallback() {
        public void run(JavaScriptSupport jss) {
            jss.addScript("alert('%s')", message);
        }
    });
}

Java (sender)

@Inject
private PushManager pushManager;

public void sayHello() {
   pushManager.broadcast("/myTopic", "Hello World!");
}
Clone this wiki locally