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 WebSocket Client to a WebSocket Endpoint

The following sections walk you through a sample scenario that demonstrates how to send a message from a WebSocket client to a WebSocket endpoint via the ESB Profile of WSO2 Enterprise Integrator (WSO2 EI):

Introduction

If you need to send a message from a WebSocket client to a WebSocket endpoint via the ESB Profile of WSO2 EI, you need to establish a persistent WebSocket connection from the WebSocket client to the ESB Profile of WSO2 EI as well as from the ESB Profile of WSO2 EI 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. Finally you need to configure the WebSocket inbound endpoint of the ESB Profile of WSO2 EI to use the created sequences and listen on port 9091.

Prerequisites

Configuring the sample scenario

  • Create the sequence for client to back-end mediation as follows:

    <sequence name="dispatchSeq">
         <property name="OUT_ONLY" value="true"/>
         <send>
             <endpoint>
                 <address uri="ws://localhost:8082/websocket"/>
             </endpoint>
         </send>
    </sequence>
  • Create the sequence for back-end to client mediation as follows:

    <sequence name="outDispatchSeq" xmlns="http://ws.apache.org/ns/synapse">
       <log level="full"/>
       <respond/>
    </sequence>
  • Configure the WebSocket inbound endpoint in the ESB Profile of WSO2 EI as follows to use the created sequences and listen on port 9091:

    <inboundEndpoint name="test" onError="fault" protocol="ws"
       	sequence="dispatchSeq" suspend="false">
       	<parameters>
           	<parameter name="inbound.ws.port">9091</parameter>
           	<parameter name="ws.outflow.dispatch.sequence">outDispatchSeq</parameter>
           	<parameter name="ws.client.side.broadcast.level">0</parameter>
           	<parameter name="ws.outflow.dispatch.fault.sequence">fault</parameter>
       	</parameters>
    </inboundEndpoint>
    

Executing the sample scenario

  • Execute the following command to start the WebSocket client:

    java -DclientPort=9091 -cp netty-example-4.0.30.Final.jar:lib/*:. io.netty.example.http.websocketx.client.WebSocketClient

Analyzing the output

If you analyze the log, you will see that a connection from the WebSocket client to the ESB Profile of WSO2 EI is established, and the sequences are executed by the WebSocket inbound endpoint.

You will also see that the message sent to the WebSocket server is not transformed, and that the response injected to the out sequence is also not transformed.