Listen to a tcp data stream from an Ionic Native App

I have device that broadcasts a stream of data over TCP. I can see the data stream from the linux command ‘nc -v ipAddress port’ that shows me something like:

Connection to 192.168.1.111 39150 port [tcp/*] succeeded!
$AIMWV,44.4,R,4.6,N,A*03
$AIHDG,116.4,,,4.0,E*2C
$AIMWV,44.4,R,4.6,N,A*03
$AIHDG,116.4,,,4.0,E*2C

I also use a few Android and iOS apps that can connect to this device.

I am trying to read this data stream from an Ionic 6 / angular / capacitor application.

From my search in the ionic forum I understand that I may not be able to access the data running the ionic application in the browser (as a Web App) however it should be possible to do it as a mobile app using a cordova plug-in (cordova-plugin-chrome-apps-sockets-tcp) that I installed.

I copied/simplified the following code from a post in the ionic forum without understanding how it uses the cordova plug-in as there is no explicit reference to it:

 (<any>window).chrome.sockets.tcp.create({}, createInfo => {
      var _socketTcpId= createInfo.socketId;
      console.log("created socket with id: "+_socketTcpId);
      (<any>window).chrome.sockets.tcp.connect(_socketTcpId, '192.168.1.111', 39150, result => { 
        if (result === 0) {
          (<any>window).chrome.sockets.tcp.onReceive.addListener( info => { 
            (<any>window).chrome.sockets.tcp.close(info.socketId);
            console.log(info.data)
          });
          (<any>window).chrome.sockets.tcp.onReceiveError.addListener( info => {
            console.log('error: ', info.data)
          });
        } else {
          console.log('not connected')
        }
      })
    })

I build the app and installed on an Android mobile phone, and I get the following error:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'sockets' of undefined
TypeError: Cannot read property 'sockets' of undefined
    at new ListenerPage (/src_app_pages_listener_listener_module_ts.js:103)
    at NodeInjectorFactory.ListenerPage_Factory [as factory] (ng:///ListenerPage/ɵfac.js:5)

I think I am just making a call to the chrome.sockets.tcp API that appears to be deprecated (chrome.sockets.tcp - Chrome Developers)

Can anyone give me any indication on how to address this problem?

Thank you for the help.

1 Like

Not sure if you are still working on this, but I am working a similar issue with Capacitor. My requirements are such that I have to have a persistent open connection, so I have come to the conclusion that the only path is to write some Native code in Java (or Kotlin) that opens a socket and waits for connection, and add this to a custom plugin. When it receives data, I am thinking I will store it in a buffer and return that buffer to the CapacitorJS “side” through periodic polling, likely using setTimeout() or similar in JS.

Chrome sockets tcp will not work for me, since Chrome sockets on Android (not ChromeOS) do not have a way to open a tcpServer socket.

Just wanted to check if anyone has any feedback on my approach, and offer my thoughts to anyone facing similar constraints.