Receiving an event when new data can be read - chrome.socket.read API

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) { // do things with data // .... // re register handler with callback itself chrome.socket.read(socketId,null,onReadHandler); } chrome.socket.read(socketId,null,onReadHandler); 
+4
source share
1 answer

For TCP connections, the callback passed to socket.read will only be executed when new data is available. The sample code mentioned has been fixed to use a callback instead of setInterval.

The online documentation is really out of date, and we are working hard to update it. If you want to avoid the risk of obsolete documents these days of quick API changes, you should always check the API reference documents - they are generated directly from the code and do not need editorial work. If you feel β€œhacked” :-), you can also look directly at the Chromium API definitions ( this one for the socket API )

Last but not least, there are Sublime Chrome Apps and Extensions for Sublime Text . It is not finished yet, but you can already get code completion, CSP verification and some templates for bootstrapping. Install it through the Sublime Package Manager .

+3
source

All Articles