Sample code for 30+ languages & platforms
React Native

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.

Chilkat React Native Downloads

React Native
import { Socket } from '@chilkat/react-native'

async function chilkatExample() {
  const socket = new Socket();

  // Connect to the server using TLS.
  const bTls = true;
  const maxWaitMs = 5000;
  try {
    await socket.connectAsync('example.com', 5000, bTls, maxWaitMs);
  } catch {
    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.
  const bAvailable = await socket.pollDataAvailableAsync();
  if (!bAvailable) {
    console.log('No data is immediately available.');
  } else {
    console.log('Data is available to read.');
  }
}