...
Add dependency to either org.apache.wicket:wicket-native-websocket-jetty9, org.apache.wicket:wicket-native-websocket-jetty, or org.apache.wicket:wicket-native-websocket-tomcat.
...
Setup the custom WicketFilter implementation for the chosen web container in your web.xml.
For Jetty 9.0+ this is
<filter-class>org.apache.wicket.protocol.ws.jetty9.Jetty9WebSocketFilter</filter-class>
For Jetty 7.5+ - 8.x this is
<filter-class>org.apache.wicket.protocol.httpws.jetty7.Jetty7WebSocketFilter</filter-class>
For Tomcat 7.0.27+:
<filter-class>org.apache.wicket.protocol.httpws.tomcat7.Tomcat7WebSocketFilter</filter-class>
...
See the demo application at martin-g's GitHub. It is written with Scala and uses Akka which are not available at Maven central repository so it cannot be hosted at Apache Git servers.
Client side APIs
By adding a WebSocketBehavior to your component(s) Wicket will contribute wicket-websocket-jquery.js library which provides some helper functions to write your client side code. There is a default websocket connection per Wicket Page opened for you which you can use like Wicket.WebSocket.send('{msg: "my message"}').
If you need more WebSocket connections then you can do: var ws = new Wicket.WebSocket(); ws.send('message');
To close a connection: Wicket.WebSocket.close() or ws.close().
Wicket.WebSocket is a simple wrapper around the native window.WebSocket API which is used to intercept the calls and to fire special events (Wicket.Event PubSub).
Event name | Arguments | Description |
---|---|---|
/websocket/open | jqEvent | A WebSocket connection has been just opened |
/websocket/message | jqEvent, message | A message has been received from the server |
/websocket/closed | jqEvent | A WebSocket connection has been closed |
/websocket/error | jqEvent | An error occurred in the communication. The connection will be closed |
If you don't need to listen for these events then you can just use the native JavaScript API (window.WebSocket).
A demo code can be seen in the Demo Application
Testing
The module provides org.apache.wicket.protocol.ws.util.tester.WebSocketTester which gives you the possibility to emulate sending and receiving messages without the need to run in a real web container, as WicketTester does this for HTTP requests.
Check WebSocketTesterTest for an example.
Differences with Wicket-Atmosphere module.
...
Currently supported web containers are Jetty 7.5+ and Tomcat 7.0.27+.
Currently supported browsers are Google Chrome/Chromium, Firefox 11+, Safari 5.x (with Jetty), IE10.
FAQ
Request and session scoped beans do not work.
The Web Socket communication is not processed by Servlet Filters and Listeners and thus the Dependency Injection libraries have no chance to export the request and session beans.