Websocket Component
Available as of Camel 2.10
The websocket component provides websocket endpoints for communicating with clients using websocket. The component uses Eclipse Jetty Server which implements the IETF specification (drafts and RFC 6455). It supports the protocols ws:// and wss://. To use wss:// protocol, the SSLContextParameters must be defined.
![]() | Version currently supported As Camel 2.10 uses Jetty 7.5.4.v20111024, only the D00 to D13 IETF implementations are available. |
URI format
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Name |
Default Value |
Description |
sslContextParameters |
|
Reference to a org.apache.camel.util.jsse.SSLContextParameters in the Registry. This reference overrides any configured SSLContextParameters at the component level. See Using the JSSE Configuration Utility. |
You can append query options to the URI in the following format, ?option=value&option=value&...
Here is some examples on How to pass the parameters
To use ssl and websocket secure --> websocket://localhost:8443?/test?sslContextParametersRef=#sslContextParameters
Message Headers
The websocket component uses 2 headers to indicate to either send messages back to a single/current client, or to all clients.
Key |
Description |
WebsocketConstants.SEND_TO_ALL |
Sends the message to all clients which are currently connected. You can use the sendToAll option on the endpoint instead of using this header. |
WebsocketConstants.CONNECTION_KEY |
Sends the message to the client with the given connection key. |
Component Options
The WebsocketComponent must be configured prior to use, to setup host, to act as a websocket server.
Option |
Default |
Description |
Host |
0.0.0.0 |
The hostname. |
Port |
9292 |
The port number. |
StaticResources |
null |
Path for static resources such as index.html files etc. |
Endpoint Options
The WebsocketEndpoint can be configured prior to use
Option |
Default |
Description |
sendToAll |
null |
To send to all websocket subscribers. Can be used to configure on endpoint level, instead of having to use the WebsocketConstants.SEND_TO_ALL header on the message. |
Usage
In this example we let Camel exposes a websocket server which clients can communicate with. The websocket server uses the default host and port, which would be 0.0.0.0:9292.
The example will send back an echo of the input. To send back a message, we need to send the transformed message to the same endpoint "websocket://echo". This is needed
because by default the messaging is InOnly.
from("websocket:)
.log(">>> Message received from WebSocket Client : ${body}")
.transform().simple("${body}${body}")
.to("websocket:);
This example is part of an unit test, which you can find here. As a client we use the AHC library which offers support for web socket as well.
See Also