Javascipt Gateway


The Javascript Gateway allows web applications to access fjåge agents. While the Javascript API is very similar to the Python Gateway API, there are a few additional steps required to setup the web services needed for the Javascript API to work. Being limited by the single threaded browser model, the Javascript API uses promises and callbacks to deliver results from APIs that may incur latency.

Enable the web sockets connector

First the web sockets connector has to be enabled (let’s say on port 8080), so that fjåge can be accessed over web sockets from a browser:

import org.arl.fjage.*
import org.arl.fjage.connectors.*

platform = new RealTimePlatform()
container = new Container(platform)
def websvr = org.arl.fjage.connectors.WebServer.getInstance(8080)
websvr.add('/fjage', '/org/arl/fjage/web')
// add any other contexts needed to serve your application here
container.addConnector(new WebSocketConnector(8080, "/ws", true))
container.add 'shell', shell
// add other agents to the container here

Use the Javascript module

It is easiest to illustrate the use of the Javascript API though a simple code example:

import { Gateway, MessageClass, Performative } from '/fjage/fjage.js';

var gw = new Gateway();


gw.agentForService('').then((aid) => {
    shell = aid;
}).catch((ex) => {
    console.log('Could not find SHELL: '+ex);

gw.addMessageListener((msg) => {
    return false;

function makeRq(shell) {
    let req = new ShellExecReq();
    req.recipient = shell;
    req.cmd = 'ps';
    gw.request(req).then((msg) => {
    }).catch((ex) => {
        console.log('Could not execute command: '+ex);

This code first opens a gateway through the web socket interface back to the web server that served this Javascript. It then imports the message class, and looks for an agent providing the service. If found, it subscribes to messages from that service and calls makeRq() to make a command execution request to the agent providing that service. The request is to execute a command “ps” and simply log the response to the browser’s console.

The user should refer to the detailed API description for the Javascript API for more information.