Module SDK and Node NAPI addon

Started by Javed Ahmadzai, May 13, 2019, 12:56 AM

Previous topic - Next topic

Javed Ahmadzai

I'm trying to write a small Node.js NAPI add-on for VCMP but I am confused how will it work with VCMP's module SDK.


I have seen the "vcmp-java-plugin" which embeds JVM and binds VCMP events to Java methods (Level 1 abstraction) and another plugin "vcmp-javascript-plugin" which is written on the top of vcmp-java-plugin that embeds JavaScript v8 engine in Java and binds JavaScript functions to java methods (Level 2 abstraction).


I very much like the "vcmp-java-plugin" since we can use Java freely and have the full advantage of the language, similarly if we could embed node in c/c++, interfaced with module SDK we might have the power of Node.js. It will also make the server development easier since many developers already know JavaScript.


There are some approaches to solve the problem.
We can write NAPI add-on for Node.js, it is nothing but c/c++ binding to Node.js, now the point is when we write Node.js add-on we have to run JavaScript code first for example: "node server.js" in terminal, it will start a Node.js script/process in a shell, but we want to run VCMP and handle the logic in Node.js, we could embed v8 engine and provide it as a dll plugin for VCMP server but that would't be effective since we cannot use the power of Node.js.

If anyone has a better idea or how to embed Node.js or use Node.js as an environment for VCMP servers please leave a clue then.

P.S: This is purely for learning purpose


ysc3839

You have to embedding nodejs in VC:MP server. Or you have to use some inter-process communication mechanism, which is not efficient.

PS: I used to considered writing a nodejs plugin, but I found it lacks document/library for embedding. While Python has boost.python and pybind11.

Javed Ahmadzai

That would be great to embed Node.js in VCMP server but you are right there is no proper documentation for that, Currently the aim is NAPI addon for Node but it would need some type of inter process communication or I don't know how the communication to VCMP server and Node.JS app will be done.


This post is good but still can't figure how they did it https://electronjs.org/blog/electron-internals-using-node-as-a-library

ysc3839

#3
Found jxcore which provides embeddable interface.
I don't know what's the difference between nodejs.

Last updated 2 years ago, a dead project.

Stormeus

Embedding Node.js in other applications isn't really well-documented, but have you looked at https://github.com/rubys/node-embed?

Javed Ahmadzai

I don't know anything about this. Stormeus why did't you aim to write VCMP Server in Node.JS it would have been a lot easier and quicker since you did't have to write hashing or sqlite modules. everything would have been provided out of the box by Node by writing VCMP Server as an add-on to Node developers would also had the advantage of Node's event loop since it is Non-blocking I/O.


Is there any VCMP documentation or repository about VCMP Server, API's and Stuff?

DizzasTeR

Because NodeJS is a crappy language atleast to me and perhaps many others, and you have to look at the general scope of the people, most people prefer a language like Lua and since Squirrel, Python are quite similar to each other Squirrel wasn't really a bad choice.

Its actually a blessing that one can even discuss extending vcmp to other languages platform because before this wasn't even possible when everyone used to be stuck at Pawn.

On topic: VCMP Server is not open-source and you don't need it either to implement a new language API for VCMP server, you may take a look at the existing language bindings (Which I believe you did), everything is on your own ;D

.

#7
The reason is quite simple. Have you ever tried to Build V8? Because that's what node.js uses. And on top of that, have you ever tried to build Node.js itself? How do you deal with package installation and the environment that node.js needs in general? Have you ever asked yourself, `just why isn't there a fork of node.js that does things differently? like so many other open-source projects out there` The answer is quite simple. It would be a tremendous burden with a very complex setup that would require a significant amount of manpower and knowledge to maintain. This could work in a closed organisation with a common setup but the same can't be said about the general public.

Some of you are still baffled by the `index 'abc' does not exist` errors. And you want the developers trying to waste that amount of time in dealing with everyone's issues on setting up the whole thing and actually using.

I mean seriously tho. Try to reason with your fantasies. This is (waaaaay) beyond the scope of this project.

EDIT:

The only way this could be simplified is to make the server into a dynamic link library that you can then load into any program. Basically the server itself would be a plugin. I remember asking something like this in the past (link).

That way, you can load the server into any process and locate the entry point to initialize it and get a structure you can control and communicate with in return.

But that would get awkward eventually and is still beyond the scope of the project itself. I mean seriously tho, don't you think there are more pressing matters that need to be resolved first (if ever).
.

Stormeus

The actual reason we didn't embed Node.js into the server is because we don't want to embed any language into the server itself. The server's main design goal is to be lightweight by default and extensible as needed. This is why even the Squirrel language, while popular in this community, is only available as a plugin.

(It also forces us to make sure we make all functionality available in the server SDK, which anyone can then use however they want. It's not limited to providing scripting languages.)

Javed Ahmadzai

#9
Thanks for your views, I know it is very hard to build V8 and embed node.js and it never was my intention, I am talking about the light weight server node add-on which one would use like this.

Usage:


npm install vcmp
const vcmp = require('vcmp');
const npmModules = require('any npm module'); // user is free to use any npm module


vcmp.createServer(config => {
     .........
});

// events
vcmp.onStart = () => {};
vcmp.onPlayerSpawn = () => {};


run the server


node server.js

The vcmp server is provided as npm package everyone can download and use it just like a normal npm package, it does't contain v8 or node itself but the bindings and vcmp server implementation in c++, it also is ABI stable it will work on all version of node.js one does't have to worry about node or v8 version that is the main advantage of napi addons.  napi addons are easy to use easy to maintain both c and c++ versions are available.
Very light-weight since the server is just a node add-on written in c/c++ and used via node javascript, no additional setup for user he just needs to install node I think almost everyone has it, there is no need to provide sqlite or other database driver they can use anything of their choice since he can use other thousands of modules in npm package repository. It might also work on web servers that have nodejs installed so servers might be hosted on web servers.

Here is an example of both c and c++ version of node add-on


Anyways since I just presented an idea and since there is no open source material to implement own version of vcmp server, I would not mind closing the thread.


Thanks for your informative replies.