This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Sending a Message from a HTTP Client to a WebSocket Endpoint
The following sections walk you through a sample scenario that demonstrates how to send a message from a HTTP client to a WebSocket endpoint via WSO2 Enterprise Integrator:
Introduction
If you need to send a message from a HTTP client to a WebSocket endpoint via WSO2 Enterprise Integrator, you need to establish a persistent Websocket connection from WSO2 Enterprise Integrator to the WebSocket back-end.
To demonstrate this scenario, you need to create two dispatching sequences. One for the client to back-end mediation, and another for the back-end to client mediation. Then you need to create a proxy service to call the created sequences.
Prerequisites
Start the WSO2 Enterprise Integrator. For information on how to start WSO2 Enterprise Integrator, see Running the Product.
Download the sample netty artifacts.
Execute the following command to start the WebSocket server on port 8082:
java -cp netty-example-4.0.30.Final.jar:lib/*:. io.netty.example.http.websocketx.server.WebSocketServer
Configuring the sample scenario
Create the sequence for client to back-end mediation as follows:
<?xml version="1.0" encoding="UTF-8"?> <sequence name="dispatchSeq" xmlns="http://ws.apache.org/ns/synapse"> <in> <property name="OUT_ONLY" value="true"/> <property name="FORCE_SC_ACCEPTED" scope="axis2" type="STRING" value="true"/> <property name="websocket.accept.contenType" scope="axis2" value="application/json"/> <send> <endpoint> <address uri="ws://localhost:8082/websocket"/> </endpoint> </send> </in> </sequence>Create the sequence for the back-end to client mediation as follows:
<sequence name="outDispatchSeq" xmlns="http://ws.apache.org/ns/synapse"> <log level="full"/> </sequence>Create a proxy service as follows to call the above sequences:
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="websocketProxy1" transports="http,https" statistics="disable" trace="disable" startOnLoad="true"> <target inSequence="dispatchSeq" faultSequence="outDispatchSeq"/> <description/> </proxy>
Executing the sample scenario
Execute the following command to invoke the proxy service:
curl -v --request POST -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body><test>Value</test></soapenv:Body></soapenv:Envelope>" -H Content-Type:"text/xml" http://localhost:8280/services/websocketProxy1Analyzing the output
If you analyze the log, you will see that a HTTP request is sent to the WebSocket server, and that the WebSocket server injects the response to the out sequence.