An attempt to use TCP sockets using the chrome.socket API in the Chrome editor version 25.0.1364.5.
Looking at the documentation for chrome.socket.read , there seems to be no way to get notified when new data is available to read.
There is some sample code for a TCP server that polls a read command every 500 ms, but I think that would be inefficient / accurate
// Start polling for reads. setInterval(this._periodicallyRead.bind(this, socketId), 500);
The more confusing is the fact that in the 'Network Communications' Documentation, in the "Receive Data" section, it is indicated that a special handler can be passed as an onEvent parameter to chrome.socket.create
A parameter is an object with a single value "onEvent", which is a function reference to a method that will be called when data is available on the port.
This onEvent parameter will be used as follows
chrome.socket.create( 'udp', '127.0.0.1', 1337, { onEvent: handleDataEvent }, // <-- call this when new data is available createHandler )
But this seems to only apply to UDP connections, as I get the following error when I try to use it
Error: Invocation of form socket.create(string, string, integer, object, function) doesn't match definition socket.create(string type, optional object options, function callback) at Object.normalizeArgumentsAndValidate (schemaUtils:119:11) at Object.<anonymous> (schema_generated_bindings:301:32) at chrome-extension://obljaojhdffbpcdfbeoiejegaodfoonp/background.js:11:15 at chrome.Event.dispatchToListener (event_bindings:387:21) at chrome.Event.dispatch_ (event_bindings:373:27) at dispatchArgs (event_bindings:249:22) at Object.app.runtime.onLaunched (app.runtime:116:7) at Object.chromeHidden.Event.dispatchEvent (event_bindings:255:35)
So the question is, can there be something similar with TCP Connections? Instead of polling the read method every x milliseconds?
Update
This is a workaround that I use until more detailed documentation / event support appears.
function onReadHandler(readInfo) {