Skip to content

Integration

Henry Burgess edited this page Mar 23, 2023 · 2 revisions

Task integration

To integrate the server functionality with a cognitive task, the task must implement functionality that allows it to exchange data with the server.

⚠️ Data exchange protocols must be established prior to implementing communications:

  1. Standardizing the format and structure of data to be sent to the server from the task. This ensures that the R script can validate any received data.
  2. Standardizing the format and structure of data to be received by the task from the server. This allows the task to utilize the data to construct dynamic behavior as defined by the server output and minimize the likelihood of issues arising during task delivery.

Sending Requests

⚠️ Sending request and awaiting responses in JavaScript is an asynchronous operation and may require refactoring of source code to ensure the cognitive task is halted while awaiting a response from the server.

JavaScript itself provides functionality to send requests asynchronously without the need for additional libraries.

const response = fetch("http://localhost/task", {
    headers: {
      "Content-Type": "application/json",
    },
    // Pass data in the request body
    body: JSON.stringify(data),
  })
  .then((response) => {
    // Parse and return the response from the server
    return response.json();
  });

You can read more about the Fetch API here.

The Axios library packages this functionality into a straighforward set of functions and parameters, and is a good alternative to the default JavaScript implementation.

// Example using Axios
const response = axios.get("http://localhost/task", {
    params: data,
  })
  .then((response) => {
    // Parse and return the response from the server
    return response.json();
  });

You can read more about the Axios library here.

Clone this wiki locally