Scripting Plugin Developer Guide

Introduction

The jWebSocket Scripting Plug-in is an extension to provide server side programming in jWebSocket using scripting languages supported by the Java VM. In the first version it is only supported the JavaScript language, however the base is already created to support new languages in the future.

JavaScript is not the must “recommended” development language for server side enterprise applications because of the language and runtimes limitations. However the JSR 223 provides full integration with Java classes and access to the objects in the Java VM instance, allowing re-using in our scripts all the power of the Java runtime platform.
In the other hands, the new JavaScript engine Nashorn promises enterprise grade byte-code convertion for JavaScript based applications, reaching ultra fast performance and stability.

A fact is: Java developers with experience in JavaScript are benefited with a high productivity level.

The Scripting Plug-In is about development productivity and simplicity while still being rugged and immense in the resources it offers to developers. If we compare the size of an application written in Java with jWebSocket (which in fact greatly simplifies Java development) and compared with the same application written in JavaScript for Scripting Plug-In, we can say that we get 40% - 80% less code and our application will be much easier to read and maintain.
The advantages offered by a dynamic and "magical" language as JavaScript running inside an instance of the Java VM are incalculable for a software factory and its developers. Scripting Plug-In supports runtime apps management features such as reload / deploy / undeploy without having to restart the server.
The ability to run scripting applications within a secure context into the Java VM (sandboxing), allows the secure development of services and clouds platforms based on the technology that offers jWebSocket and ScriptingPlugIn. 

The "app" concept

The Scripting Plug-in introduces a new concept into the jWebSocket development environment, it is: "script application" or simply "app".
An app into the scripting plug-in is a high level abstraction of script codes (see JSR-223) being running into an isolated and secure program context, so multiple apps can run concurrently totally independent with each other.
Script apps has not file structure limitations only two required files located at the root level of the application folder, those are the bootstrap app file named "App." + extension (i.e App.js), and the application manifest file "manifest.json" as deployment descriptor. 

NOTE: Experimental NPM support is available by setting "npm_enable: true" in the manifest file.
 

The manifest file:

{
  // script app programming language extension
  "language_ext": "js", // script app programming language extension
  // required jWebSocket version
  "jws_version": "1.0.0", 
  // required jWebSocket plug-ins (dependencies)
  "jws_dependencies": [
    "jws.system"
    "jws.filesystem:1.0.0"
  ], 
  // required security permissions
  "permissions": [ 
    "permission java.io.FilePermission \"${APP_HOME}\\-\", \"read,write,delete\""
  ],
  // application author
  "author": "Innotrade",
  // flag that enable/disable NPM (Nashorn compliant) modules to be used inside JavaScript apps (default: false)
  "npm_enabled": false,
  // application description
  "description": "Application description...",
  // enable/disable script applications (default: true)
  "enabled": true
}

An app is deployed only if the requirements described in the manifest file(such as permissions and dependencies) are satisfied.

A script app cannot change it's sandbox security permissions during execution, just advice to the Scripting PlugIn about it permission's requirements during loading, is reponsability of the system administrator to grant or not the permissions to the app.

The basic "echo" app example

The basic "echo" app in the server (App.js):

App.publish("main", {
  sayHello: {
    authenticated: true,
    handler: function(aName){
      return "Hello '" + aName + "', from a JavaScript app ;)";
    }
  }
});

The calling from the client:

client.callScriptAppMethod("echo", "main", "sayHello", ["Innotrade"], {
  OnSuccess: function(aResponse){
    alert(aResponse.result);
  }
});

Generating the app in the browser:

echo = client.getScriptApp("echo", function(App){
  echo.main.sayHello("Innotrade", function(aResponse){
    // aResponse.result
  });
});

In the above code example, we have created an "echo" app in a few lines that counts with all the added value offered by jWebSocket such as "connection/session management", "authentication/authorization", "logging", "performance"..., it sounds simple right? ;)

The gold benefit for software companies of using the ScriptingPlugIn is the minimal learning curve. Making developers with medium Java and JavaScript level to increase development productivity.

Publications

Learn more about WebSockets in general, get background information and gain deeper insight!

Join jWebSocket

Wether developer, designer or translator – join the jWebSocket team and grow together with our success!

Copyright © 2013 Innotrade GmbH. All rights reserved.