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, WSO2 ESB deﬁnes a WebSocket subprotocol extension to determine the content type of WebSocket frames.
WSO2 ESB's WebSocket inbound endpoint supports the following Synapse subprotocols by default:
Now let's look at a sample scenario that demonstrates WebSocket to WebSocket integration using subprotocols to support content handling.
This scenario includes the following sections:
Let's say you need to send messages between two WebSocket based systems using WSO2 ESB as a WebSocket gateway that facilitates the messaging, and you also need to read and transform the content of WebSocket frames that are sent and received.
The following should take place in this scenario:
- The WebSocket Client sends WebSocket frames to WSO2 ESB.
- When the initial handshake happens between the WebSocket client and WSO2 ESB WebSocket inbound endpoint, the WebSocket client sends a
Sec-WebSockets-Protocolheader that specifies the content type of the WebSocket frame. In this sample it is
- WSO2 ESB's WebSocket inbound endpoint 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 ESB sends the transformed message in the form of WebSocket frames.
If necessary, you can use the data mapper to perform data transformation inside 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.
- Start WSO2 ESB. For information on how to start the ESB server. 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:
Configuring the sample scenario
Create the sequence for client to back-end mediation as follows:
websocket.accept.contenTypeproperty to inform the WebSocket sender to build the frames with the specified content type, and to include the same subprotocol header that was used to determine the content of the WebSocket frames. In this case it is JSON.
Create the sequence for back-end to client mediation as follows:
Configure WSO2 ESB's WebSocket inbound endpoint as follows to use the created sequences and listen on port 9091:
Executing the sample scenario
Open a terminal, navigate to the location where you saved the netty artifacts, and execute the following command to start the WebSocket client:
You will see the message
WebSocket Client connected !on the client terminal.
Send the following sample JSON payload from the client terminal:
Analyzing the output
When you send a sample JSON payload from the client, you will see that a connection from the WebSocket client to WSO2 ESB is established, and that the ESB receives the message. Following is the ESB log that you will see:
This shows that the sequences are executed by the WebSocket inbound endpoint.
You will also see that the message sent to the WebSocket server is transformed, and that the response injected to the out sequence is also transformed.
Following is the server log that you will see:
Following is the client log that you will see: