DukeScript with Node.js backend
In one of our latest projects the customer has decided to use Node.js as a backend. So we decided to share a minimal demo how simple it is using DukeScript APIs.
You’ll need node installed and in addition the websocket module. Here’s the server, we’re using a websocket to make it a little fancier.
The websocket server sends a simple JSON message every 5 seconds after we connected. Don’t worry too much about the server code. This is just for demo purposes. Now we need a Java model to read the message:
DukeScript has the extremely convenient @OnReceive annotation, that takes care of serializing messages to and deserializing messages from the server. So we extend our class with:
This will generate a method in our Model class with all the boilerplate code. To establish the connection you once call the method with a null argument.
After that you can call the receive method with data to send and receive messages via the websocket. The websocket can also push back messages to you. Let’s add a @Function, so we can send a message from the UI:
Now we only need a UI to start the connection, so we add this to our View:
The full code to play with is available on Github.
And if you want to code the Node based server in Java as well, have a look at this project: Node4J.
That’s it. Easy, isn’t it? Have fun coding DukeScript!