Skip to main content

TCP Service

TCP services handle raw TCP connections and are suitable for custom binary or text-based protocol implementations where HTTP overhead is not acceptable. WSO2 Integrator generates a service with a pre-defined onConnect handler that returns a connection service instance to manage per-connection lifecycle events.

note

TCP service support is currently in beta.

Creating a TCP service

  1. In the design view, select Add Artifact.
  2. Select TCP Service under Integration as API.
  3. Fill in the creation form fields and select Create.
TCP Service creation form showing the TCP Port field and Advanced ConfigurationsTCP Service creation form showing the TCP Port field and Advanced Configurations
FieldDescriptionDefault
TCP PortPort on which the TCP service listens. Required.80

Expand Advanced Configurations to set the Listener Name (default: tcpListener).

After selecting Create, WSO2 Integrator opens the service in the TCP Service Designer and generates the onConnect handler automatically.

TCP Service Designer

After creating the service, WSO2 Integrator opens the TCP Service Designer. The designer shows:

  • Listener pill: the attached listener (for example, tcpListener).
  • Event Handlers section: the handlers that define how the service responds to connection events. The onConnect handler is pre-generated when the service is created.
TCP Service Designer showing the tcpListener pill and the onConnect event handler rowTCP Service Designer showing the tcpListener pill and the onConnect event handler row

Select Configure in the service header to open the service configuration view.

Service configuration

In the TCP Service Designer, select Configure to open the TCP Service Configuration panel. The left navigation shows TCP Service and its attached listeners.

TCP services do not have service-level configuration fields. Select the listener entry (for example, tcpListener) under Attached Listeners to configure the listener settings.

Select + Attach Listener at the bottom of the panel to attach an additional listener to the service.

Listener configuration

The listener binds to a port and manages incoming TCP connections.

In the TCP Service Configuration panel, select the listener entry under Attached Listeners to configure the listener.

FieldDescriptionDefault
Local PortPort number the listener binds to. Required.
Local HostHostname or IP address the listener binds to.""
Secure SocketTLS/SSL configuration. Configure this to enable secure connections.()

Select + Attach Listener at the bottom of the panel to attach an additional listener or select an existing named listener.

Implementing connection logic

TCP connection logic is split across two objects. The main service handles the onConnect event and returns a connection service instance. The connection service class implements the per-connection handlers: onBytes, onClose, and onError.

onConnect handler

The onConnect handler runs when a new client connects. Its job is to instantiate the connection service class and return it to the runtime.

Select the onConnect row in the TCP Service Designer to open the flow designer for that handler.

The generated flow includes two steps:

  • A Declare Variable step that instantiates the connection service class (for example, new TcpConnectionService()).
  • A Return step that returns the connection service instance to the runtime.

Add logic before the return to log the connection or pass context to the connection service constructor.

Connection service class

The onBytes, onClose, and onError handlers live inside the connection service class, not in the main service.

Finding the connection service class

In the onConnect flow designer, select the Declare Variable step. The right panel shows the variable details, including the Type field that identifies the connection service class (for example, TcpEchoService).

Opening the type diagram

In the sidebar, expand Types and select the connection service class name (for example, TcpEchoService). The Types view opens and shows the type diagram for that class, listing its methods: onBytes, onError, and onClose.

Types view showing the TcpEchoService type node with onBytes, onError, and onClose methodsTypes view showing the TcpEchoService type node with onBytes, onError, and onClose methods

Opening the Service Class Designer

Select the type node in the diagram to open the Service Class Designer. The designer shows:

  • Class Variables: shared state available across all handler methods. Select + Variable to add one.
  • Methods: the onBytes, onError, and onClose remote functions generated for the connection service.

Select any method row to open its flow designer and define the handler logic.

Service Class Designer showing Class Variables and Methods for TcpEchoService with onBytes, onError, and onClose remote functionsService Class Designer showing Class Variables and Methods for TcpEchoService with onBytes, onError, and onClose remote functions

Connection lifecycle callbacks

CallbackSignatureTriggerTypical use
onConnect(tcp:Caller caller) returns tcp:ConnectionService|tcp:ErrorNew TCP client connectsInstantiate and return the connection service
onBytes(readonly & byte[] data) returns byte[]|tcp:Error? or (tcp:Caller caller, readonly & byte[] data) returns tcp:Error?Data received from the clientEcho bytes back directly, or use caller->writeBytes() for explicit writes
onClose()Client disconnectsRelease per-connection resources
onError(tcp:Error err)Connection error occursLog and handle the error condition

What's next

  • HTTP service — build REST endpoints for request/response integrations
  • WebSocket service — handle full-duplex connections over HTTP upgrade
  • Connections — configure TCP client connections to call external services
  • Mocking — replace TCP clients with controlled stubs in tests