Calling MATLAB in JavaScript - javascript

I'm new to JavaScript and is coding a simple webpage game using JavaScript for my research. People can play the game and some data will be collected. The collected data needs to be processed by MATLAB and the results will be returned to the JavaScript code. The JavaScript code will be run on client-side and the MATLAB will be in server-side.
How can a JavaScript program call functions in MATLAB and also receive the data from MATLAB? I searched online and found that MATLAB can be used as an Automation server and then be called by web applications (http://www.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-a-web-application.html).
The link gives an example using VBScript to call MATLAB files. However, I cannot find any resource for using JavaScript to call. What should I do with JavaScript? Besides, is there a way that JavaScript can interact with MATLAB?

Your question is somewhat vague. It is a Web app, so there are two parties involved, the server side and the client side. Does MATLAB need to be called at the server side or the client side?
Assuming it is server side, there are multiple options
1) The COM automation server you mentioned works only if the server is running Windows, because COM is for Windows only.
2) Interface the server side code with C/C++ somehow, for example, using CGI, and use the C/C++ code to invoke the MATLAB Engine.
3) Use MATLAB Builder JA to convert MATLAB code to a JAR file, which can be invoked from your Java-based web app. Similarly, you can use MATLAB Builder EX to convert MATLAB code to a .NET assembly DLL, which can be invoked from your C# based (ASP.NET) web app.
4) Set up an MATLAB Production Server (MPS) and deploy code using MATLAB Compiler. The deployed MATLAB code can be invoked using Java, Python or .NET client code that run inside your server-side web app.
If it is client side JavaScript talking to MATLAB, I am afraid there is no easy way to make the two talk directly. MPS is still a possibility, but it will be via the server-side web app. Perhaps you can explore the possibility of using MATLAB Coder to convert the MATLAB code into C code and then use something like a transpiler to convert the C code into JavaScript code that can be run directly in the browser.

I think that you can create a client programs using the RESTful API and JSON:
Step 1: Write MATLAB Code
Step 2: Create a deployable archive with the production server compiler app
Step 3: Place the deployable archive on a server
Step 4: Enable cross-origin resource sharing (CORS) on the server
Step 5: Write JavaScript code using the RESTful API and JSON
Step 6: Embed JavaScript within the HTML code
Step 7: Run the example
These steps were taken from this link:
Example: Web-Based Bond Pricing Tool Using JavaScript

Related

Is possible a web app that execute Pandas/Scipy/Numpy in the client?

I wrote a data processing program in python. My program takes some input data and transforms it using python libraries such as PYTHON-MIP, Numpy, Scipy, Pandas, etc. I want to build a graphical interface for this program and I would like it to be web to display graphs with libraries like Chart JS. I know how to build this app by separating the frontend and the backend. But I would like the processing to run on the client. That is, I want an application without a backend. I don't know what options I have.
The browser (the "client") is not able to run python code. It will have to run on the server side.
With web assembly is possible execute python in the client. The best option is pyodide, that able the numpy, scipy, scikitlearn, pandas, etc. Pyodide

How can I call a JavaScript library at only server side with Spring Boot?

I need to call JavaScript library on server side, which result will be returned to the user through Java.
This library uses many external JavaScript library, so it's not a single .js file.
Couple of options, but without more context, we can only guess.
Nashorn/GraalVM to execute Javascript from Java, but it depends on the library you're trying to invoke
Install Node.js on the server, write a Node.js script which uses that lib and invoke the script from Java. Depending on the complexity of the use case and how often you're trying to do this, maybe write a microservice in Node.js and call it from Java via REST.

Running python on client side

I want to run python code on client-side in my web technology project. The objective is that there will be some input from the user, taken via JavaScript, is then fed to .py files which will produce some output to be displayed on the website at the same time.
Please suggest a suitable way like brython or skulpt. A brief explanation or quick links will be very generous.
I'll be using turtle, numpy, manim, matplotlib and such libraries is that is the concern.
You may be able to build and compile some application to run on the client machine, but modern web browsers do not have the ability to run python files. As mentioned in the comments, you'd have to get user input and then process it server side.

Can .NET DLLs be created using Javascript or JScript.NET?

I'm creating a video game that has single player and multiplayer. The single player is done in C# (Unity), and want to use Gamesparks BaaS that works on Javascript (NodeJS) for their server code.
Since I want the multiplayer server to be authoritative, I need the server code to run Javascript and the client will run C#, which means duplicated code
But, if I can create .NET dlls in Javascript I can reduce code duplication heavily by using those libraries in Unity and using the Javascript code on the server.
Thanks!
If you want to use javascript (and by javascript I mean nodejs), along with c# and communicate between those two, you can use electron-edge. It helps you to run c# code using nodejs through Electron API.
Here is the link of their Github url: Electron-Edge-documentation
You can also interact with dlls, using an creating your application as an electron app. You refer this question where you will find some more details regarding it.

Understanding some aspects of node.js

There are a couple of aspects of node.js I don't quite understand. I hope someone can make things clearer
When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.
Does node.js replace client side javascript?
How does node.js interact with HTML? For example if I wanted to put data from the server into this div element <div id="content"></div>
In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>
Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.
Whats confusing me is that because it is run from the server then I expect that I can use javascript on the browser to get data from the node.js server. However, examples I seen this is never done.
Thanks in advance
When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.
Wherever you want. node.js doesn't serve static content, it runs JavaScript. You tell it which script to run when you start it.
You could write some JavaScript that serves static content, but where you would keep it depends on the code you wrote.
Does node.js replace client side javascript?
Only in so far as any server side programming replaces client side JavaScript.
One advantage of using JS on the server side is that you can reuse libraries on both the client and the server. See Mojito for a framework that claims to focus on this (I haven't had time to try it myself yet).
For example if I wanted to put data from the server into this div element <div id="content"></div> In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>
PHP is a template language with an embedded programming language. JavaScript is a programming language. Typically you would use a template language (e.g. moustache) from your JS.
Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.
Yes, if you want to. Just like any other server side programming environment. (Assuming you are using node to run an HTTP server).
Node.js is not a server (like e.g. Apache). It's a platform to run Javascript with some built in libraries (so-called modules). It is very easy to write server (HTTP or any other) in Node.js, but you can also write completely different programs (no network related, meant to be executed locally).
I suggest you read this: http://www.nodebeginner.org/. It took me several hours but allowed me to understand basics of Node without much pain.
As for client side scripting, it's generally separate. Code in Node runs in separate environment then the one in browser. They can communicate, but you have to explicitly make them to. It's not much different from server side coding in PHP. The code on server produces some output (eg. HTML) which is sent to client. If there are scripts in output, client (browser) executes it. They can communicate (via XHR, websockets, etc.), but by itself those scripts are separate.
How does node.js interact with HTML? For example if I wanted to put
data from the server into this div element In
PHP you could do something like this:
You would probably send the content as JSON to the JS-Client and insert it into the DOM (using plain JS or JQuery).
I've wrote a REALLY trivial (and not quite feature-rich :-P) chat application in Node.js a while a go, to try out some concepts and understand working with JS on both the client and server. Maybe it will give you some clues.
EDIT
In this application, the server also serves static files, which you should not do when implementing production ready application (Node is not really suited for serving static files!).

Categories