Sample code for 30+ languages & platforms
JavaScript

Poll a Socket for Available Data

See more Socket/SSL/TLS Examples

Demonstrates Socket.PollDataAvailable, which performs a nonblocking check for readable activity and returns whether data or another read-ready condition is present.

Background. This lets an application check for incoming data without blocking, for example in an event loop that services multiple tasks.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

var socket = new CkSocket();

//  Connect to the server using TLS.
var bTls = true;
var maxWaitMs = 5000;
success = socket.Connect("example.com",5000,bTls,maxWaitMs);
if (success == false) {
    console.log(socket.LastErrorText);
    return;
}

//  Perform a nonblocking check for readable data.  Returns true when data or another read-ready
//  condition is present, and false when nothing is immediately available.
var bAvailable = socket.PollDataAvailable();
if (bAvailable !== true) {
    console.log("No data is immediately available.");
}
else {
    console.log("Data is available to read.");
}