TCP sockets in ionic


you need to create an arraybuffer and send this, not a string.




Indeed, you need to send an arraybuffer. So, please note that in the sendPacket function within the code attached to the thread, there is a str2arrayBuffer function that does exactly this conversion.


thanks you,
last question, in php i use :
$print = chr(0x1B).chr(0x64).chr(0x00);
can you explain me how to correctly write/convert this sequence

thanks you

the solution is here :


thanks you for all

I tried using the same code in my ionic 2 typescript project and still no success. It says can not read property sockets of undefined.

Please assist me with some sample application. Struggling to get the reference in typescript for this plug in.

Do you still no success?? I got same problem…

Is the undefined problem resolved? Can’t seem to find other solutions on the net…

I used the same code in ionic 2 but it return Runtime error can’t read property ‘sockets’ of undefined.

you need to use the platform.ready() function to make sure cordova is ready, THEN the chrome sockets will be usable…

import {  Platform } from 'ionic-angular';

inject via constructor

constructor(public navCtrl: NavController, public navParams: NavParams,
  			public viewCtrl: ViewController, **platform:Platform**)

in constructor

	(readySource) => {
	     	 // start handling network stuff now

Can you help me to sole this error. i am using the exact code that you have shared.

main.js:1507 ERROR TypeError: Cannot read property ‘sockets’ of undefined
at HomePage.addDevice (file:///android_asset/www/build/main.js:48894:22)
at Object.eval [as handleEvent] (ng:///AppModule/HomePage.ngfactory.js:5159:24)

i had a problem where I had to reinstall the cordova platfom

ionic cordova platform rm (ios or android)
ioinc cordova plafform add (ios or android)

and then I had a problem where the plugin wasn’t install properly with npm

so I had to do npm install chrome-sockets…

see the top of
says use npm i

Hi guy,
I’m a new development guy, and I’ve found a solution and I give it.

export class HomePage  {

private socketTcpId: number;

constructor(platform:Platform , public navCtrl: NavController, private vibration: Vibration,   public alertCtrl: AlertController) {
        platform.ready().then((readySource) => {
	     	 // start handling network stuff now
             (<any>window).chrome.sockets.tcp.create({}, createInfo => {
             this.socketTcpId = createInfo.socketId;   
                // this.AlertAction();;
                (<any>window).chrome.sockets.tcp.connect(this.socketTcpId, "", 12345, result => {
                console.log("Connected to server");

And Send Method

arrayBuffer2str(buf): string {
		var str= '';
		var ui8= new Uint8Array(buf);
		for (var i= 0 ; i < ui8.length ; i++) {
			str= str+String.fromCharCode(ui8[i]);
		return str;

   str2arrayBuffer(str) : ArrayBuffer {
		var buf= new ArrayBuffer(str.length);
		var bufView= new Uint8Array(buf);
		for (var i= 0 ; i < str.length ; i++) {
			bufView[i]= str.charCodeAt(i);
		return buf;

	sendPacket( ) : void {
				var data2send= this.str2arrayBuffer( "Philip is the best\n\r");
				// connection ok, send the packet
				(<any>window).chrome.sockets.tcp.send(this.socketTcpId, data2send);

Enjoy it :slight_smile:

Hello TOTO69,

do you have any idea how to receive data from the server if the server sends data/answer back after you send the server data with the send method ?

you would use the receiveListener

(window).chrome.sockets.tcp.onReceive.addListener(recvListener) ;

var amountReceived = 0;
function recvListener(info)
amountReceived +=;
var arr = new Uint8Array(;
var textChunk = String.fromCharCode.apply(null,arr);

note that for large messages you might have multiple receives to get the whole message

hi sdetweil, thankyou for your fast answer but it seems like it is not working. here is my code maybe you can have a quick look:

var data = this.str2ab(“0x7c0x040x000x00”);
(window).chrome.sockets.tcp.create({}, createInfo => {
let socketTcpId = createInfo.socketId;
(window).chrome.sockets.tcp.connect(socketTcpId, this.IPADRESS, this.PORT, result => {
console.log(“Connected to server”);
alert(“connected to server”);
(window).chrome.sockets.tcp.send(socketTcpId, data, function(info) {
alert(“send executed”);
(window).chrome.sockets.tcp.onReceive.addListener(recvListener) ;
var amountReceived = 0;
function recvListener(info)
amountReceived +=;
var arr = new Uint8Array(;
var textChunk = String.fromCharCode.apply(null,arr);

i think it connect and send work fine because result and info.resultCode are equal 0. But i am having problems receiving data from server

i believe the socket is set to paused mode on open
so you need to turn that off.

(window).chrome.sockets.tcp.setPaused(SocketId, false);

also, you should setup the receive handler BEFORE you send, as there would be a race condition if the
sender sent before the receive was ready… (it might cause the connection to break)

the server side just sends back ,right… doesn’t open a NEW connection back…

yes it just sends back. in this case it should send back the same data sent with send method.
do you have any idea how/where to start with the receive handler? sorry but i don’t have much programming exp, i am really new at it.

put some console.log stmts in to see what is going on.

make sure to move the addListener before the send
and also the setPauseD(socket, false)
before the send

yes i did put them before the send but doesn’t work. i did some debugg and i saw that the onReceive.addListener method does not get called.
directly after send method is called comes an error : errors.js:31 Connection reset by peer

The sender closed the connection before u could read the data