How to inject module from different app in Node.js How to inject module from different app in Node.js node.js node.js

How to inject module from different app in Node.js


You can create a package from one of your apps and then reference the package in the other app.

https://docs.npmjs.com/getting-started/creating-node-modules


You will have to use vm module to achieve this. More technical info here https://nodejs.org/api/vm.html. Let me explain how you can use this:

  1. You can use the API vm.script to create compiled js code from the code which you want run later. See the description from official documentation

Creating a new vm.Script object compiles code but does not run it. The compiled vm.Script can be run later multiple times. It is important to note that the code is not bound to any global object; rather, it is bound before each run, just for that run.

  1. Now when you want to insert or run this code, you can use script.runInContext API.

Another good example from their official documentation:

'use strict';const vm = require('vm');let code =`(function(require) {   const http = require('http');   http.createServer( (request, response) => {     response.writeHead(200, {'Content-Type': 'text/plain'});     response.end('Hello World\\n');   }).listen(8124);   console.log('Server running at http://127.0.0.1:8124/'); })`; vm.runInThisContext(code)(require);

Another example of using js file directly:

var app = fs.readFileSync(__dirname + '/' + 'app.js');vm.runInThisContext(app);

You can use this approach for the conditional code which you want to insert.


There are several ways to decouple two applications. One easy way is with pub/sub pattern (in case you don't need a response).
(Now if you have an application that is very couple, it will be very difficult to decouple it unless you do some refactoring.)
zeromq offers a very good implementation of pub/sub and is very fast.
e.g.

import zmq from "zmq";socket.connect('tcp://127.0.0.1:5545');socket.subscribe('sendConfirmation');socket.on('message', function (topic, message) {    // you can get the data from message.    // something like:    const msg = message.toString('ascii');    const data = JSON.parse(msg);    // do some actions.    // .....});//don't forget to close the socket.process.on('SIGINT', () => {    debug("... closing the socket ....");    socket.close();    process.exit();});//-----------------------------------------import zmq from "zmq";socket.bind('tcp://127.0.0.1:5545');socket.send(['sendConfirmation', someData]);process.on('SIGINT', function() {  socket.close();});

This way you could have two different containers (docker) for your modules, just be sure to open the corresponding port.
What i don't understand, is why you inject wsSocket and also you create a new Socket. Probably what I would do is just to send thesocket id, and then just use it like:

const _socketId = "/#" + data.socketId;     io.sockets.connected[socketId].send("some message");

You could also use another solution like kafka instead of zmq, just consider that is slower but it will keep the logs.
Hope this can get you an idea of how to solve your problem.