TCPSocket
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.
The TCPSocket API allows applications to directly connect with external devices over TCP sockets.

Checking if TCPSocket is supported

A TCPSocket.info() function is available to check if the user's device has access to the TCP sockets. It returns an object:
Field
Type
Description
supported
boolean
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}, readHandler, closeHandler).
Parameter
Description
host
String hostname of socket, e.g. '10.0.0.1'.
port
Number of the port of the socket, e.g. 8080.
readHandler
Optional callback that gets called when data is present on the socket.
closeHander
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.
JavaScript
TypeScript
var socketHost = '192.168.1.1';
var socketPort = 8080;
โ€‹
var readHandler = function(data) {
// data received from TCP socket
console.log(data);
}
โ€‹
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) {
console.log(error);
}
var socketHost = '192.168.1.1';
var socketPort = 8080;
โ€‹
var readHandler = function(data) {
// data received from TCP socket
console.log(data);
}
โ€‹
var closeHandler = function(error) {
if (error) {
console.log("Error", error);
}
}
// use a try .. catch, since an exception may be thrown.
try {
var tcpSocket = await TCPSocket.connect({host: socketHost, port: socketPort}, readHandler, closeHandler);
} catch (error) {
console.log(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:
JavaScript
TypeScript
var stream = mySocket.stream;
var streamReader = stream.getReader();
โ€‹
function startRead() {
setTimeout(function() {
streamReader.read().then(function(dataRead) {
if (dataRead.done) {
console.log("Stream finished");
} else {
// use data:
console.log("Data", dataRead.value);
โ€‹
// trigger another read:
startRead();
}
โ€‹
// 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:
}).then(journey.forceDigest);
}, 1000);
}
โ€‹
// call the startRead function once:
startRead();
var stream = mySocket.stream;
var streamReader = stream.getReader();
โ€‹
function startRead() {
setTimeout(async function() {
var dataRead = await streamReader.read();
โ€‹
if (dataRead.done) {
console.log("Stream finished");
} else {
// use data:
console.log("Data", dataRead.value);
โ€‹
// 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:
journey.forceDigest();
โ€‹
// trigger another read:
startRead();
}
}, 1000);
}
โ€‹
// call the startRead function once:
startRead();
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:
JavaScript
TypeScript
var stream = mySocket.stream.pipeThrough(new TextDecoderStream());
โ€‹
stream.read().then(function(data) {
if (!data.done) {
// result is String
console.log(data.value);
}
});
var stream = mySocket.stream.pipeThrough(new TextDecoderStream());
โ€‹
var data = await stream.read();
โ€‹
if (!data.done) {
// result is String
console.log(data.value);
}

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.
JavaScript
TypeScript
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]);
tcpSocket.write(binaryData);
var tcpSocket = await TCPSocket.connect({host: socketHost, port: socketPort}, readHandler, closeHandler);
โ€‹
// writing binary data ("Hello" in hexadecimal)
var binaryData = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
await tcpSocket.write(binaryData);

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 modified 1mo ago
Copy link
On this page