Home

Spacecode JS API


This API allows JavaScript developers to operate Spacecode Remote Devices through their web applications. Reading the section about the backward-compatibility is highly recommended for users of old browsers.


Download


The latest release is available on the general Spacecode SDK Documentation.


Usage


We wanted the Spacecode JS API to be a cutting-edge JavaScript experiment. Thus, it offers the choice to be implemented either as a global object, or as an AMD module.


In the global namespace


In the HTML file:

<script src="spacecode.js"></script>

Once the script is loaded, a global object Spacecode is created in the root namespace.

Using it with JavaScript:

var device = new Spacecode.Device("192.168.1.20");

 device.on('connected', function()
 {
     console.log('connected');
     device.release();
 });

device.on('disconnected', function()
{
    console.log('disconnected');
});

device.connect();


As an AMD module


With an AMD loader like requirejs:

In the HTML file:

<script data-main="app" src="require.js"></script>

In the app.js file:

// Load the module 'spacecode' (spacecode.js)
define(['spacecode'], function(Spacecode)
{
    var device = new Spacecode.Device("192.168.1.20");

     device.on('connected', function()
     {
         console.log('connected');
         device.release();
     });

    device.on('disconnected', function()
    {
        console.log('disconnected');
    });

    device.connect();
});

Assuming require.js, app.js and spacecode.js in the same repository


Backward-compatibility


Spacecode JS API uses some of the latest web standards, like the WebSocket API or the new data structures from ECMAScript 6. For this reason, the API is not compatible with old browsers. Regarding the WebSocket support, the website Can I Use provides a list of the compatible browsers. For the ES6 support (especially the Map global object), an up-to-date table is available here.