Listening to TCP Messages from multiple applications via the same TCP Port with WSO2 Micro Integrator

Tanya Madurapperuma
3 min readAug 11, 2021

--

Introduction

There can be different occasions where we have to use transport layer protocols like TCP over the application layer protocols for message passing. This article explains how you can leverage the TCP transport support in WSO2 Micro Integrator (MI) to listen to messages from multiple applications using the same TCP port.

Solution Overview

We’ll be using a switch mediator in the mediation flow to route the TCP messages to the correct integration logic. In order to use this approach, each TCP message should contain some sort of unique identifier to distinguish, from which application each of these TCP messages are coming from, so that the mediation logic can switch them to respective integration logics or backends.

Solution Walkthrough

Follow the below steps to implement the solution described above.

Enable TCP Transport

Go to the deployment.toml file of the WSO2 MI located at conf directory of the product distribution and add the following configuration to enable the TCP transport for receiving and sending TCP messages.

[transport.tcp]
listener.enable = true
sender.enable = true

TCP Message Listener Proxy

You can use a proxy like below to listen to TCP messages.

  • Under the transports parameter provide “tcp” as the transport.
<proxy name="listenerProxy" startOnLoad="true" transports="tcp" xmlns="http://ws.apache.org/ns/synapse">
  • And then specify the listening TCP port and other required parameters for the proxy as shown below. Since we want a single TCP port to listen for messages from multiple applications we have specified only a single port (6062) here.
<parameter name="transport.tcp.responseClient">true</parameter>    <parameter name="transport.tcp.inputType">string</parameter>    <parameter name="transport.tcp.recordDelimiter">?</parameter>    <parameter name="transport.tcp.contentType">application/xml</parameter>    <parameter name="transport.tcp.port">6062</parameter>    <parameter name="transport.tcp.recordDelimiterType">character</parameter>
  • Afterwards use a switch mediator to route incoming messages from the multiple applications into the correct receiver. Here we have assumed that application name is part of the TCP messages that is sent to the listener proxy as shown in the below sample message.
<?xml version="1.0" encoding="UTF-8"?> 
<app>
<name>app1</name>
<message>
<!--rest of the tcp message-->
</message>
</app>?

Because of that we have used an xpath expression as below as the source of the switch mediator.

<switch source="//app/name">
  • And then inside each case of the switch mediator we can define what we want to do with the incoming message. In the above sample listener proxy we have sent the TCP message of the app1 to another listener proxy called ReceiverProxy while in other cases we have just logged the app name.

Sending TCP Messages to another Listener Proxy

As shown in the ListenerProxy above we can define the TCP url of the receiving proxy as an address uri to send TCP messages.

<send>                        
<endpoint>
<address uri="tcp://localhost:6065/services/ReceiverProxy">
<!--other endpoint configurations -->
</address>
</endpoint>
</send>

Proxy that is listening to the incoming TCP messages from the ListenerProxy is ReceiverProxy. And port in the address uri is listener port that is defined in the ReceiverProxy.

A sample ReceiverProxy is as provided below. And for the demonstration purposes this proxy only logs the incoming message but you can define any integration logic that you wants inside the inSequence of the ReceiverProxy.

Note that the transport.tcp.port parameter contains the port (6065) that was configured in the address uri of the previous proxy which acts as a TCP sender in this moment.

Conclusion

WSO2 Micro Integrator supports both sending and receiving TCP messages. TCP messages that are send from different applications can be configured to consume using different TCP ports or using the same TCP port depending on the requirement.

--

--