Version and platform compatibility

  • This feature was introduced in version 21.1.1 of the JourneyApps Container for iOS, Android and Desktop, and requires version 4.78.0 or greater of the JourneyApps Runtime.

  • TCPSocket support is currently only available for customers on the Enterprise plan.

The TCPSocket API allows applications to directly connect with external devices over TCP sockets.

Checking if TCPSocket is supported

A function is available to check if the user's device has access to the TCP sockets. It returns an object:




Indicates if TCPSocket is supported on the current device. Not supported on Web

Connecting to a TCP socket

To connect to a TCP socket, the host and port of the socket is required.

To connect, use var tcpSocket = TCPSocket.connect({host: host, port: port, timeout: timeout}, readHandler, closeHandler).



String hostname of socket, e.g. ''.


Number of the port of the socket, e.g. 8080.


Number value in milliseconds. This value overrides the timeout period, which closes the TCP socket connection after network inactivity. Its default value is 10 seconds.

Note: This parameter was introduced in version 4.86.6 of the JourneyApps Runtime.


Optional callback that gets called when data is present on the socket.


Optional callback that gets called when an error occurs or socket closes. The closeHandler will have an optional error parameter that will be a string, or null/undefined when there isn't an error.

var socketHost = '';
var socketPort = 8080;

var readHandler = function(data) {
  // data received from TCP socket

var closeHandler = function(error) {
  if (error) {
    console.log("Error", error);
// use a try .. catch, since an exception may be thrown.
try {
  var tcpSocket = TCPSocket.connect({host: socketHost, port: socketPort}, readHandler, closeHandler);
} catch (error) {

Reading data

There are two ways to read data from the socket.

The readHandler from the example above uses a callback with data in Uint8Array when there is data available.

A ReadableStream object is also available. To use the ReadableStream, the following can be used:

var stream =;
var streamReader = stream.getReader();

function startRead() {
  setTimeout(function() { {
      if (dataRead.done) {
      console.log("Stream finished");
    } else {
      // use data:
      console.log("Data", dataRead.value);

      // trigger another read:

    // in the case where view variables are changed,
    // e.g. updating text on the view,
    // we need to call the journey.forceDigest() function
    // due to this code running inside a setTimeout:
  }, 1000);

// call the startRead function once:

Data must be continually read when using the ReadableStream. In the example above, the data is read one second after previous reading.

ReadableStream methods can be used, including piping through other streams, e.g. TextDecoderStream:

var stream = TextDecoderStream()); {
  if (!data.done) {
    // result is String

Writing to a socket

The TCPSocket.connect function returns a TCPSocket instance. This instance is used to write data to the socket in binary (Uint8Array) format.

var tcpSocket = TCPSocket.connect({host: socketHost, port: socketPort}, readHandler, closeHandler);

// writing binary data ("Hello" in hexadecimal)
var binaryData = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);

Closing and resetting

To close a connection, call close() on the connection object, e.g. tcpSocket.close().

To reset all the sockets, call TCPSocket.reset(). This closes all open sockets.

There is also an isOpen property on each connection object, e.g. tcpSocket.isOpen that returns a boolean to check if the socket is open. Note that it may take some time after a socket has disconnected for the socket itself to register that it has closed.

Last updated