~~NOTRANS~~
~~Title: Push support~~
Sometimes it's important to notify all client about an event or about changed records. This is known as "push". You push a message to all clients. In web environments a websocket is used for push notification. In JVx we have a [[jvx:communication:push_publish|push mechanism]] which is technology independent and it works different in different UI technologies.
How to push a message to other clients?
In our example, we have a button and on button press we send a simple reload notification to all other clients. This is a super simple task.
The action event for the button looks like:
public void doSendNotification(UIActionEvent pEvent) throws Throwable
{
//saves all changes
save();
//triggers push
getConnection().callAction("doReloadAllClients");
}
In our [[jvx:server:lco:lifecycle|life-cycle]] object, we need the ''doReloadAllClients'' method:
public void doPublish()
{
ICallBackBroker broker = SessionContext.getCurrentCallBackBroker();
Thread th = new Thread(new Runnable()
{
public void run()
{
broker.publish("RELOAD", null, PublishMode.AllOtherMasterSessions);
}
});
th.start();
//with JVx 3.0, not additional thread is required
//SessionContext.getCurrentCallBackBroker().publish("RELOAD", null, PublishMode.AllOtherMasterSessions);
}
The reload event needs a handler. This handler/listener will be registered in our application:
public class CustomApplication extends Application
implements ICallBackResultListener
{
@Override
public void setConnection(AbstractConnection pConnection)
{
AbstractConnection con = getConnection();
if (con != null)
{
con.removeCallBackResultListener(this);
}
super.setConnection(pConnection);
pConnection.addCallBackResultListener(this);
}
public void callBackResult(CallBackResultEvent pEvent)
{
if ("RELOAD".equals(pEvent.getInstruction()))
{
try
{
reload();
}
catch (Throwable th)
{
error(th);
}
}
}
}
That's it.
But be careful, because push won't work in load-balanced applications with this standard implementation!