
* *Classpath dependency*

Depending on the web container that is used the application has to add a dependency to either:

- for Jetty 9.0.x
[source,java]
----
<dependency>
  <groupId>org.apache.wicket</groupId>
  <artifactId>wicket-native-websocket-jetty9</artifactId>
  <version>...</version>
</dependency>
----

- for Tomcat 7.0.27+ (the old, non-JSR356 implementation)
[source,java]
----
<dependency>
  <groupId>org.apache.wicket</groupId>
  <artifactId>wicket-native-websocket-tomcat</artifactId>
  <version>...</version>
</dependency>
----

- for JSR356 complaint implementations (at the moment are supported: Tomcat 8.0+, Tomcat 7.0.47+, Jetty 9.1.0+ and JBoss Wildfly 8.0.0+)
[source,java]
----
<dependency>
  <groupId>org.apache.wicket</groupId>
  <artifactId>wicket-native-websocket-javax</artifactId>
  <version>...</version>
</dependency>
----

- for https://projects.spring.io/spring-boot/[Spring Boot] applications also add
----
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
----


NOTE: All web containers providing JSR356 implementation are built with Java 7. This is the reason why _wicket-native-websocket-javax_ module is available only with Wicket 7.x. If your application runs with JRE 7.x but you are still using Wicket 6.x then you can
use the https://github.com/wicketstuff/core/tree/wicket-6.x/wicketstuff-native-websocket-javax[WicketStuff] module. Beware that the API/implementation of _wicket-native-websocket-javax_ may change before Wicket 7.0.0 is released!

NOTE: The examples above show snippets for Maven's pom.xml but the application can use any other dependency management tool like http://www.gradle.org/[Gradle]

* *web.xml*

In _WEB-INF/web.xml_ replace the usage of *WicketFilter* with any of the following depending on the web container that is used:

For Jetty 9.0.x:
[source,java]
----
<filter-class>org.apache.wicket.protocol.ws.jetty9.Jetty9WebSocketFilter</filter-class>
----

For Jetty 7.5+ and 8.x:
[source,java]
----
<filter-class>org.apache.wicket.protocol.ws.jetty7.Jetty7WebSocketFilter</filter-class>
----

For Tomcat 7.0.27+ (old implementation):
[source,java]
----
<filter-class>org.apache.wicket.protocol.ws.tomcat7.Tomcat7WebSocketFilter</filter-class>
----

For JSR356 complaint web containers (at the moment: Tomcat 7.0.47+, Tomcat 8.x and Jetty 9.1.x):
[source,java]
----
<filter-class>org.apache.wicket.protocol.ws.javax.JavaxWebSocketFilter</filter-class>
----

For https://projects.spring.io/spring-boot/[Spring Boot] application:
----
@Bean
    public FilterRegistrationBean wicketFilter() {
        final FilterRegistrationBean wicketFilter = new
FilterRegistrationBean();
        wicketFilter.setDispatcherTypes(DispatcherType.REQUEST,
DispatcherType.ERROR, DispatcherType.FORWARD, DispatcherType.ASYNC);
        wicketFilter.setAsyncSupported(true);
        wicketFilter.setFilter(new JavaxWebSocketFilter());
        wicketFilter.addInitParameter(WicketFilter.APP_FACT_PARAM,
SpringWebApplicationFactory.class.getName());
        wicketFilter.addInitParameter(WicketFilter.FILTER_MAPPING_PARAM,
"/*");
        wicketFilter.addUrlPatterns("/*");
        return wicketFilter;
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Bean
    public WicketServerEndpointConfig wicketServerEndpointConfig() {
        return new WicketServerEndpointConfig();
    }
----

* *WebSocketBehavior*

_org.apache.wicket.protocol.ws.api.WebSocketBehavior_ is similar to Wicket Ajax behaviors that you may have used.
Add WebSocketBehavior to the page (or to any component in the page) that will use web socket communication:

[source,java]
----
public class MyPage extends WebPage {
 
  public MyPage()
  {
    add(new WebSocketBehavior() {
      @Override
      protected void onMessage(WebSocketRequestHandler handler, TextMessage message)
      {
        String msg = message.getText();
        // do something with msg
      }
    });
  }
}
----

Use _message.getText()_ to read the message sent by the client and use _handler.push(String)_ to push a text message to the connected client. Additionally you can use _handler.add(Component...)_ to add Wicket components for re-render, _handler.prependJavaScript(CharSequence)_ and _handler.appendJavaScript(CharSequence)_ as you do with _AjaxRequestTarget_.

* *WebSocketResource*

Wicket allows one thread at a time to use a page instance to simplify the usage of the pages in multithreaded enviroment. When a WebSocket message is sent to a page Wicket needs to acquire the lock to that page to be able to pass the _IWebSocketMessage_ to the _WebSocketBehavior_. This may be problematic when the application needs to send many messages from the client to the server.
For this reason Wicket provides _WebSocketResource_ - an IResource implemetation that provides the same APIs as _WebSocketBehavior_. The benefit is that there is no need of synchronization as with the pages and the drawback is that _WebSocketRequesthandler.add(Component...)_ method cannot be used because there is no access to the components in an _IResource_.

To register such WebSocket resource add such line to _YourApplication1.init()_ method:
[source,java]
----
getSharedResources().add("someName", new MyWebSocketResource());
----

and 
[source,java]
----
  page.add(new BaseWebSocketBehavior("someName"));
----
to any page. This will prepare the JavaScript connection for you.

* *WebSocket connection registry*

To push data to one or more clients the application can use the _IWebSocketConnectionRegistry_ to find all registered connections and send data to all/any of them:

[source,java]
----
Application application = Application.get(applicationName);
WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application);
IWebSocketConnectionRegistry webSocketConnectionRegistry = webSocketSettings.getConnectionRegistry();
IWebSocketConnection connection = webSocketConnectionRegistry.getConnection(application, sessionId, key);
----

