Bunny-Spawner, a Node.JS and Bonita BPM example with kitties and bunnies
This is an example of how to use the Bonita BPM web REST API with Node.js. To make it warm and fuzzy, we used some good old 8bit like sprites with cats and rabbits lol
Prerequisite
- clone git repositiry GitHub - Bonitasoft-Community/Bunny-Spawner-Node-JS-and-Bonita-BPM-example-with-kitties-and-bunnies: This is an example of how to use the Bonita BPM web REST API with Node.js. To make it fun and adorable for your girl friend, we used some good old 8bit like sprites with cats and rabbits ;-) lol
- node.js
- npm Package socket.io
- npm Package unirest
- Bonita BPM Community 6.3.1 on localhost:8080
- added user âadminâ with password âbpmâ
- intalled process bunny-1.0.bar
how to use
- start the bunny process on the Bonita server, via the Portal
- In the Bunny-Spawner-Node-JS-and-Bonita-BPM-example-with-kitties-and-bunnies folder, start the node server by typing:
node server.js
- start Internet explorer Nooo⊠please donât. Start Firefox or Chrome instead.
- Go on localhost:8042
- have fun ! right click for kitties and left click for bunnies.
how it works
it a three parts system,
- Browser js client
- Node js server
- Bonita BPM
The browser client and the Node.js server communicate throught socket with Socket.io.
The Node.js server call the Bonita REST API with Unirest.
And Bonita call the Node.js server via HTTP requests.
Bonita BPM process named âbunnyâ is the following:
Spawner process simply execute a connecteur (cf. Bonita BPM TO Node.js server)
Browser js client TO Node.js server
the following code allow the client to send notification to the server (line 107: game.js & line 19/24: handlerLoader.js)):
socket = io.connect(document.URL);
socket.emit(âupdateâ, {varName: âbunnyâ, value: 1});
socket.emit(âupdateâ, {varName: âcatâ, value: 1});
this code receive the notification on server side (line 77: server.js):
socket.on(âupdateâ, function(data) {
if (data.varName == âbunnyâ)
bunny += data.value;
if (data.varName == âcatâ)
cat += data.value;
âŠ
Node.js server TO Browser js client
the following code allow the server to send notification to clients (line 40: server.js):
toClient.emit(âupdateâ, { type: keyval[0], val: keyval[1] });
this code receive the notification on client side and add at a random position a bunny or a kitty (line 108: game.js):
this.socket.on(âupdateâ, function(data) {
console.log(âupdate receivedâ);
console.log(data.type + ", " + data.val)
switch(data.type) {
case âbunnyâ:
var x = Math.floor((Math.random() * 1200) + 40);
var y = Math.floor((Math.random() * 400) + 40);
self.addBunny(x, y);
break;
case âcatâ:
var x = Math.floor((Math.random() * 1200) + 40);
var y = Math.floor((Math.random() * 400) + 40);
self.addCat(x, y);
break;
default:
break;
}
});
Node.js server TO Bonita BPM
This connection use the package unirest for Node.js, for more information follow the link to
Unirest.
To use Bonita REST API you need to be connected so there is this fonction which allows to connect
then do your call and finally disconnect (line 166: server.js):
function connectThen(callback) {
unirest.post(remoteBonitaHost + â/loginservice?redirect=false&username=admin&password=bpmâ)
.headers({
âAcceptâ: âapplication/jsonâ
})
.jar(true)
.end(function(response) {
console.log(response.body);
callback(function() {
unirest.get(remoteBonitaHost + â/logoutserviceâ)
.headers({
âAcceptâ: âapplication/jsonâ
})
.jar(true)
.end(function() {
console.log(âdisconnect from rest APIâ);
});
});
});
}
Then, three nested request are called throught the function modValue (line 103: server.js),
- Get the process instance
- Get value of the requested variable
- Put the new value for the variable
function modValue(vari, newValue) {
connectThen(function(logout) {
> unirest.get(remoteBonitaHost + "/API/bpm/case?p=0&c=10&s=bunny")
.headers({
'Accept': 'application/json'
})
.jar(true)
.end(function(response) {
console.log("Bonita Game case ID request");
console.log(response.body);
var processId = response.body[0].id;
> unirest.get(remoteBonitaHost + "/API/bpm/caseVariable/"+processId+"/"+vari)
.headers({
'Accept': 'application/json'
})
.jar(true)
.end(function(response) {
console.log("request for " + vari);
console.log(response.body);
var value = parseInt(response.body.value) + parseInt(newValue);
> unirest.put(remoteBonitaHost + "/API/bpm/caseVariable/"+processId+"/"+vari)
.headers({
'Accept': 'application/json'
})
.send("[{\"type\":\"java.lang.Integer\",\"value\":"+value+"}]")
.jar(true)
.end(function(response) {
console.log("request for change " + vari + " with value " + value);
console.log(response.body);
logout();
});
});
});
});
}
**NOTE: you need to send: "[{\"type\":\"java.lang.Integer\",\"value\":"+value+"}]" to change the
value in order to make it work. it's a String of an array of a Json "[{ ... }]".**
Bonita BPM TO Node.js server
The comunication is done by a simple HTTP request in a custom connector on a bunny process step.
itâs made throught the following code:
if (bunny > 5) {
update = true;
bunny++;
request += "bunny=1";
}
if (cat > 2) {
i = r.nextInt(100 - cat);
if (i <= 5) {
if (update)
request += "&";
update = true;
cat++;
request += "cat=1";
}
}
if (update) {
try {
> URL url = new URL(request);
> InputStream is = url.openStream();
> is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
setNewBunnyNb(bunny);
setNewCatNb(cat);
**Enjoy and have fun ! **
praise for Konami