Javascript Setup

Ensure you have MATRIX CORE installed, before moving on.

Installing Node.js

This setup will go through how to install Node.js and the dependencies needed to create a Node application that can communicate with MATRIX CORE.

Run the following commands on your MATRIX device(Raspberry Pi) to install Node Version Manager which will then be used to install version 8.6 of Node.js.

It is strongly recommended to use version 8.6 of Node.js

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
. ~/.bashrc
nvm install 8.6


Creating A Node.js Application

Making Your Project Directory

Use the following commands to initialize a Node project folder, in the home directory ~/ of your MATRIX device.
cd ~/
mkdir js-matrix-core-app
cd js-matrix-core-app
npm init

Installing npm Packages for ZMQ and Protocol Buffers

While staying inside your app folder, use the commands below to install the ZMQ and MATRIX Protocol Buffers npm packages. This allows you to interact with MATRIX Core through Node.js.

npm install zeromq --save
npm install matrix-protos --save


Check If Everything Works

Creating app.js

To ensure your installation has succeeded, create a file named app.js and paste the code below.

// Set Initial Variables \\
var zmq = require('zeromq');// Asynchronous Messaging Framework
var matrix_io = require('matrix-protos').matrix_io;// Protocol Buffers for MATRIX function
var matrix_ip = '127.0.0.1';// Local IP
var matrix_everloop_base_port = 20021// Port for Everloop driver
var matrix_device_leds = 0;// Holds amount of LEDs on MATRIX device

// ERROR PORT \\
var errorSocket = zmq.socket('sub');// Create a Subscriber socket
errorSocket.connect('tcp://' + matrix_ip + ':' + (matrix_everloop_base_port + 2));// Connect Subscriber to Error port
errorSocket.subscribe('');// Subscribe to messages
// On Message
errorSocket.on('message', (error_message) => {
    console.log('Error received: ' + error_message.toString('utf8'));// Log error
});

// DATA UPDATE PORT \\
var updateSocket = zmq.socket('sub');// Create a Subscriber socket
updateSocket.connect('tcp://' + matrix_ip + ':' + (matrix_everloop_base_port + 3));// Connect Subscriber to Data Update port
updateSocket.subscribe('');// Subscribe to messages
// On Message
updateSocket.on('message', (buffer) => {
    var data = matrix_io.malos.v1.io.EverloopImage.decode(buffer);// Extract message
    matrix_device_leds = data.everloopLength;// Save MATRIX device LED count
});

// KEEP-ALIVE PORT \\
var pingSocket = zmq.socket('push');// Create a Pusher socket
pingSocket.connect('tcp://' + matrix_ip + ':' + (matrix_everloop_base_port + 1));// Connect Pusher to Keep-alive port
pingSocket.send('');// Send a single ping

// BASE PORT \\
var configSocket = zmq.socket('push');// Create a Pusher socket
configSocket.connect('tcp://' + matrix_ip + ':' + matrix_everloop_base_port);// Connect Pusher to Base port

// Create an empty Everloop image
var image = matrix_io.malos.v1.io.EverloopImage.create();

// Loop every 50 milliseconds
setInterval(function(){
    // For each device LED
    for (var i = 0; i < matrix_device_leds; ++i) {
        // Set individual LED value
        image.led[i] = {
            red: Math.floor(Math.random() * 200)+1,
            green: Math.floor(Math.random() * 255)+1,
            blue: Math.floor(Math.random() * 50)+1,
            white: 0
        };
    }

    // Store the Everloop image in MATRIX configuration
    var config = matrix_io.malos.v1.driver.DriverConfig.create({
        'image': image
    });

    // Send MATRIX configuration to MATRIX device
    if(matrix_device_leds > 0)
        configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(config).finish());
},50);

Running app.js

Once you have the app.js code copied, use the following command to run a simple hello world app.

node app.js

Result

Next Steps

Now that everything is properly installed, learn more about the Everloop and other Driver Protocols MATRIX Core has to offer, or view the available Javascript examples.