Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Questo contenuto non è disponibile nella lingua selezionata.
Chapter 159. Websocket
Websocket Component Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
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. Camel 2.11 uses Jetty 7.6.7.
URI format Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
websocket://hostname[:port][/resourceUri][?options]
websocket://hostname[:port][/resourceUri][?options]
You can append query options to the URI in the following format,
?option=value&option=value&...
Component Options Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The
WebsocketComponent
can 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. If this option has been configured, then a server is started on the given hostname and port, to service the static resources, eg such as an index.html file. If this option has not been configured, then no server is started. |
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.
|
|
enableJmx
|
false
|
If this option is true, Jetty JMX support will be enabled for this endpoint. |
sslKeyPassword
|
null
|
Consumer only: The password for the keystore when using SSL. |
sslPassword
|
null
|
Consumer only: The password when using SSL. |
sslKeystore
|
null
|
Consumer only: The path to the keystore. |
minThreads
|
null
|
Consumer only: To set a value for minimum number of threads in server thread pool. |
maxThreads
|
null
|
Consumer only: To set a value for maximum number of threads in server thread pool. |
threadPool
|
null
|
Consumer only: To use a custom thread pool for the server. |
Endpoint Options Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The
WebsocketEndpoint
can be configured prior to use
Option | Default | Description |
---|---|---|
sslContextParametersRef
|
Deprecated and will be removed in Camel 3.0: 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. Use the sslContextParameters option instead
|
|
sslContextParameters
|
Camel 2.11.1: 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.
|
|
sendToAll
|
null
|
Producer only: 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.
|
staticResources
|
null
|
The root directory for the web resources or classpath. Use the protocol file: or classpath: depending if you want that the component loads the resource from file system or classpath. |
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.
|
enableJmx
|
false
|
If this option is true, Jetty JMX support will be enabled for this endpoint. See Jetty JMX support for more details.
|
sslKeyPassword
|
null
|
Consumer only: The password for the keystore when using SSL.
|
sslPassword
|
null
|
Consumer only: The password when using SSL.
|
sslKeystore
|
null
|
Consumer only: The path to the keystore.
|
minThreads
|
null
|
Consumer only: To set a value for minimum number of threads in server thread pool.
|
maxThreads
|
null
|
Consumer only: To set a value for maximum number of threads in server thread pool.
|
threadPool
|
null
|
Consumer only: To use a custom thread pool for the server.
|
Message Headers Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
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. |
Usage Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
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.
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.
Here is another example where webapp resources location have been defined to allow the Jetty Application Server to not only register the WebSocket servlet but also to expose web resources for the browser. Resources should be defined under the webapp directory.
from("activemq:topic:newsTopic") .routeId("fromJMStoWebSocket") .to("websocket://localhost:8443/newsTopic?sendToAll=true&staticResources=classpath:webapp");
from("activemq:topic:newsTopic")
.routeId("fromJMStoWebSocket")
.to("websocket://localhost:8443/newsTopic?sendToAll=true&staticResources=classpath:webapp");
Setting up SSL for WebSocket Component Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
Using the JSSE Configuration Utility Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
As of Camel 2.10, the WebSocket component supports SSL/TLS configuration through the Camel JSSE Configuration Utility. This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels. The following examples demonstrate how to use the utility with the Cometd component.
Programmatic configuration of the component Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
Spring DSL based configuration of endpoint Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
Java DSL based configuration of endpoint Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
- Twitter Websocket Example demonstrates how to poll a constant feed of twitter searches and publish results in real time using web socket to a web page.