Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

If you need to read and transform the content of WebSocket frames, the information in incoming WebSocket frames is not sufficient because the WebSocket protocol does not specify any information about the content-type of frames that flow through WebSocket channels. Hence, the ESB Profile of WSO2 Enterprise Integrator defines (WSO2 EI) supports a WebSocket subprotocol extension to determine the content type of WebSocket frames.WSO2 Enterprise Integrator's

The WebSocket inbound endpoint of the ESB Profile supports the following Synapse subprotocols by default:

...

Let's say you need to send messages between two WebSocket based systems using WSO2 Enterprise Integrator the ESB Profile of WSO2 EI as a WebSocket gateway that facilitates the messaging, and you also . Let's also assume that you need to read and transform the content of WebSocket frames that are sent and received.

...

  • The WebSocket Client sends WebSocket frames to WSO2 Enterprise Integratorthe ESB Profile of WSO2 EI.
  • When the initial handshake happens between the WebSocket client and WSO2 EI the WebSocket inbound endpoint of the ESB Profile, the WebSocket client sends a Sec-WebSockets-Protocol header that specifies the content type of the WebSocket frame. In this sample it is synapse(contentType='application/json').
  • WSO2 Enterprise Integrator's The WebSocket inbound endpoint determines endpoint of the ESB Profile determines the content-type of the incoming WebSocket frame using the subprotocol.
  • Once the handshake is complete, the WebSocket inbound endpoint builds all the subsequent WebSocket frames based on the content-type specified during the initial handshake.
  • WSO2 Enterprise Integrator The ESB Profile of WSO2 EI sends the transformed message in the form of WebSocket frames.
Tip

If necessary, you can use the data mapper to perform data transformation inside WSO2 Enterprise Integrator the ESB message flow. For example, you can perform JSON to JSON transformation. To do this, you have to explicitly apply the required data mapping logic for all WebSocket frames.

Prerequisites

  • Start WSO2 Enterprise Integratorthe ESB Profile of WSO2 EI. For information on how to start the ESB Profile of WSO2 Enterprise Integrator, see Running the Product.
  • Download the netty artifacts.
  • Open a terminal, navigate to the location where you saved the netty artifacts, and execute the following command to start the WebSocket server on port 8082:

    Code Block
    java -cp netty-example-4.0.30.Final.jar:lib/*:. io.netty.example.http.websocketx.server.WebSocketServer

...

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

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

    Code Block
    languagexml
    <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>

...

  • Open a terminal, navigate to the location where you saved the netty artifacts, and execute the following command to start the WebSocket client:

    Code Block
    java -DsubProtocol="synapse(contentType='application/json') -DclientPort=9091 -cp netty-example-4.0.30.Final.jar:lib/*:.io.netty.example.http.websocketx.client.WebSocketClient

    You will see the following message WebSocket Client connected ! on the client terminal:

    Code Block
    WebSocket Client connected!
  • Send the following sample JSON payload from the client terminal:

    Code Block
    ("sample message":"test"}

...

When you send a sample JSON payload from the client, you will see that a connection from the WebSocket client to WSO2 Enterprise Integrator the ESB Profile of WSO2 EI is established, and that WSO2 Enterprise Integrator the ESB Profile of WSO2 EI receives the message. Following is the WSO2 Enterprise Integrator the log that you will see:

This shows that the sequences are executed by the WebSocket inbound endpoint.

...