Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

How to use the Swarm Javascript SDK

hanshenSun edited this page Jan 20, 2022 · 1 revision

Welcome to the CORE.Swarm.jspkg wiki!

Here is a repo of Swarm SDK examples: https://github.com/tt-acm/CORE.Swarm.WebSamples

Digest of Swarm SDK example

The explanation below uses the simple_addition_example script as an example.

1. Import Swarm SDK and Rhino3dm (if required).

// This example simplify adds two numbers together, with tree inputs
// Swarm App: https://swarm.thorntontomasetti.com/app/5f998551dd6d710004c38ca4/info
var Swarm = require('@ttcorestudio/swarm');

2. Initialize swarm app instance.

Set document unit and tolerance, and get appToken from the swarm app settings page.

Units

Swarm Compute Server is unit agnostic, results should be similar no matter what unit is being used. You can find a list of units supported by Swarm here: https://github.com/tt-acm/CORE.Swarm.Library/blob/fd9547e077510266edcfac3306d13d87d2fbb6a2/CORE.Swarm.GhIO/SwarmGhDocInterface.cs#L85

swarmApp.setDocument(8, 0.001); // Set Document unit and tolerance
swarmApp.logging = true;
swarmApp.appToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MzYyNTAwMDkzODIsImV4cCI6MTYzNjI1NTE5MzM4MiwicHJvamVjdElkIjoiNWY5OTg1NTFkZDZkNzEwMDA0YzM4Y2E0In0.43Nk21NG8PGA6PiJ_UF3pW7VPWHHqht1hQTEzTOfKYo";

3. Construct Swarm app inputs.

// Declare inputs first
let input_A = new Swarm.Input("A", "Number");
let input_B = new Swarm.Input("B", "Number");

// Add values to inputs
input_A.addData(0, [13, 14]);
input_A.addDataTree(1, 15);
input_B.addData(0, [13,14]);

swarmApp.inputs.push(input_A);
swarmApp.inputs.push(input_B);

4. Run this Swarm app.

// Sending to Swarm for compute
swarmApp.compute().then(output => {
  if (output == null) return console.log("No compute result came back.");
  let val = output.outputs;

  console.log("There are " + val.length + " inputs in this compute");

  let outputA = output.outputs[0];
  console.log("Output A has " + outputA.branches.length + " branches", outputA);
});

Please note, for computes longer than 25 seconds, please use the the runLongCompute() function instead of compute()