Asynchronous communication with Web Sockets
Wicket 6.0 introduces two new modules, both of them related to Server Push technology, Wicket-Atmosphere and Wicket Native WebSockets. In this article we will see how to make your application more dynamic by employing Wicket Native WebSockets.
Wicket Native WebSocket
Server side APIs
Wicket Native WebSocket module provides an integration that makes it as easy to use WebSockets as you already use Ajax in your Wicket applications. Basically all you need to do is to add a WebSocketBehavior to a page/component and override the callback methods you may be interested in - onConnect, onClose and/or onMessage.
Most probably an application will need to push messages from the server to the client asynchronously, i.e. without the client to ask for such messages. This may be achived by storing the needed information to lookup the WebSocket connection when WebSocketBehavior#onConnect(ConnectedMessage) is called:
Later when you need to push a message asynchronously you may lookup the connection with:
Client side API
By default when WebSocketBehavior is used Wicket will open a default WebSocket connection for you, so you can just use:
to send a message from the client to the server.
To listen for messages pushed by the server you have to subscribe for a topic with name '/websocket/message':
At Martin Grigorov's GitHub repository you may find an application that demonstrates some of the new possibilities available with WebSockets. The application schedules an asynchronous task at the server side that pushes data back to the client and visualizes it in Google Line chart.
More complete documentation may be found at the Wiki. Feel free to contribute to make it better. Just create an account and edit it!