chrome.socket 'data' event

January 12, 2013

Having previously worked with sockets in node.js I expected the chrome.socket api to have a 'data' event similar to the one in node.js net.Socket, however this is not the case. This event is emmited when new data is available to be read.

To emulate the node.js socket behaviour in chrome.socket I suggest using this workaround:

function onReadHandler(readInfo) {
     // do things with data
     // ....

     // re register handler with callback itself
     chrome.socket.read(socketId,null,onReadHandler);         
}

chrome.socket.read(socketId,null,onReadHandler);

I tried looking for better alternatives on Stack Overflow but this appears to be the best solution

http://stackoverflow.com/q/14193201/149636