Mantis Websocket API

Some desktops have trouble dealing with SSE. In such a case you can use WebSocket to connect to a Job (other API features are only available via the Mantis REST API).

The WebSocket API runs from the same servers as the Mantis REST API, and you can reach it by using the ws:// or wss:// protocol and port 7102.

Connecting to Job Output (Sink)

  • Append this to the WebSocket URI to connect to a Job by name: /jobconnectbyname/JobName

  • Append this to the WebSocket URI to connect to a specific running Job ID: /jobconnectbyid/JobID

Upon connecting, the server starts writing messages that are coming in from the corresponding Jobs.

You can append query parameters to the WebSocket URI (preceded by “?”) as is the case in a REST API. Use this to pass any Sink parameters your Job accepts. All Jobs accept the “sampleMSec=mSecs” Sink parameter which limits the rate at which the Sink output is sampled by the server.

Submitting and Connecting to Job Output

  • Append this to the WebSocket URI to submit and connect to the submitted Job: /jobsubmitandconnect

Note that you will have to send one message on the WebSocket, which is the same JSON payload that you would send as a POST body for the equivalent REST API. The server then submits the Job and then starts writing messages into the WebSocket as it gets output from the Job.

You can append query parameters to the WebSocket URI (preceded by “?”) as is the case in a REST API. Use this to pass any Sink parameters your Job accepts. All Jobs accept the “sampleMSec=mSecs” sink parameter which limits the rate at which the Sink output is sampled by the server.

Example: Javascript to Connect a Job

<!DOCTYPE html>  
<meta charset="utf-8" />  
<title>WebSocket Test</title>  
<script language="javascript" type="text/javascript">  
var wsUri = "ws://your-domain.com/jobconnectbyname/YourJobName?sampleMSec=2000";
var output;  
function init() { 
  output = document.getElementById("output"); 
  testWebSocket(); 
}
function testWebSocket() { 
  websocket = new WebSocket(wsUri);
  websocket.onopen = function(evt) { onOpen(evt) }; 
  websocket.onclose = function(evt) { onClose(evt) }; 
  websocket.onmessage = function(evt) { onMessage(evt) }; 
  websocket.onerror = function(evt) { onError(evt) }; 
}  
function onOpen(evt) {
  writeToScreen("CONNECTED"); 
}
function onClose(evt) { 
  writeToScreen("DISCONNECTED"); 
}
function onMessage(evt) {
  writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
}
function onError(evt) {
  writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message) { 
  websocket.send(message); 
}
function writeToScreen(message) { 
  var pre = document.createElement("p"); 
  pre.style.wordWrap = "break-word"; 
  pre.innerHTML = message; output.appendChild(pre); 
}
window.addEventListener("load", init, true);  
</script>  
<h2>WebSocket Test</h2>  
<div id="output"></div>

Example: Javascript to Submit a Job and Connect to It

<!DOCTYPE html>  
<meta charset="utf-8" />  
<title>WebSocket Test</title>  
<script language="javascript" type="text/javascript">  
var wsUri = "ws://your-domain.com/jobsubmitandconnect/"; 
var output;  
function init() { 
  output = document.getElementById("output"); 
  testWebSocket(); 
}
function testWebSocket() { 
  websocket = new WebSocket(wsUri);
  websocket.onopen = function(evt) { onOpen(evt) }; 
  websocket.onclose = function(evt) { onClose(evt) }; 
  websocket.onmessage = function(evt) { onMessage(evt) }; 
  websocket.onerror = function(evt) { onError(evt) }; 
}  
function onOpen(evt) { 
  writeToScreen("CONNECTED"); 
  // Change this to your job's submit json content.
  // See job submit REST API above for another example.
  doSend("{\n" +
                "  \"name\":\"Outliers-mock3\",\n" +
                "  \"version\":\"\",\n" +
                "  \"parameters\":[],\n" +
                "  \"jobSla\":{\"runtimeLimitSecs\":0,\"durationType\":\"Transient\",\"userProvidedType\":\"{\\\"unique\\\":\\\"foobar\\\"}\"},\n" +
                "  \"subscriptionTimeoutSecs\":\"90\",\n" +
                "  \"jobJarFileLocation\":null,\n" +
                "  \"schedulingInfo\":{\"stages\":{\"1\":{\"numberOfInstances\":1,\"machineDefinition\":{\"cpuCores\":1.0,\"memoryMB\":2048.0,\"diskMB\":1.0,\"scalable\":\"true\"}}}\n" +
                "}");
}  
function onClose(evt) { 
  writeToScreen("DISCONNECTED"); 
}
function onMessage(evt) {
  writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
}
function onError(evt) {
  writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message) { 
  websocket.send(message); 
}
function writeToScreen(message) { 
  var pre = document.createElement("p"); 
  pre.style.wordWrap = "break-word"; 
  pre.innerHTML = message; output.appendChild(pre); 
}
window.addEventListener("load", init, true);  
</script>  
<h2>WebSocket Test</h2>  
<div id="output"></div>