Forward get request from one node.js server to another - javascript

I currently have two instances of nodejs servers running, both listening on localhost, with instance 1 on port 15000 and instance 2 on port 16000. The first is going to work as a master with the second as part of a group of slaves, where any request coming into the first gets forwarded to the second.
I'm having trouble sending any messages from the first to the second.
var jquery = require('jquery');
jquery.get('http://localhost:16000');
called from the first does not get received by the second (jquery is loaded correctly). I'm about to try Mootools, but would like some advice on the best way to forward an incoming nodejs request directly to another instance of a nodejs server.

You want cluster.
You simply call cluster with an instance of a http.Server or net.Server and it does the load balancing for you.
If you want to roll out something yourself then call your clients with http.request which is a sensible way to send a HTTP request to a particular server in node.js.
Using jQuery or MooTools to do this for you is horrible (They don't use native C goodness, like standard node.js modules do!). Don't do this. The only reason why you would want jQuery / MooTools in node is to manipulate jsdom

Related

Making web server run scripts

I'm sure a 100 people asked this before but I couldn't find the right key words to google it.
Simply put: How do I make a web client send information to the web server the right way?
I have a simple website set up with apache 2 on a raspberry with ubuntu. Something very basic. I'd like to have a button on my website that makes my server run a script (I wrote that script in c++ but I don't care if I have to translate it). One solution is using JS client-side to send a message to my server on a specific port (say 50000), and having the server listen on that port with a custom listener.
That works fine, but I'm sure there is a right way to do this. How should I do this so that people won't be pissed by my architecture too much? (+using other ports than 80 and 443 on browser may not work if the client blocks other ports)
What you want to do is create a RESTful web API. The server can listen on a specific port and handle different HTTP requests in different ways. You will want to use Controllers, Services, and potentially a Data Access Object layer. You could either have the script you want to run in code, or you could have your code simply make a call to execute a bash or shell script once you get a valid request of the correct type (GET, POST, PATCH, PUT, etc) with the correct parameters.
https://www.tutorialspoint.com/nodejs/nodejs_restful_api.htm
You're correct, it would be a bad decision to try to have a single endpoint on one port do one thing, and another endpoint on another port do something else. First, a single application can only listen on a single port (or at least should only listen on a single port), so you'd need to spin up a new application for everything you want your back-end server to do. Second, you can't be semantic. Your users would have to look at a dictionary of port - action mappings, instead of being able to (for example) send a request to yourService.com/run/script/1234 to run a script with ID of 1234.
Here is a bit of information on HTTP requests to get you started: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

How can I write data to the client connected to a Node.js socket server from another function?

I'm attempting to make a function that would allow for triggering a script in a V8 instance of Node from a node-chakracore instance. I'm doing this by running a newer version of Node as a child process. I want to do this in a way that it only has 1 newer Node instance instead of creating another one for every script I want to run. I want to have a runV8Script function that would trigger the script on the V8 side. The way that I'm wanting to trigger scripts is by having an IPC socket, the server side being Chakra and the client side being V8.
The issue that I'm having is that I can't think of how I could use client outside of the server callback which means I can't have a function send data.
I'm just creating this server with the simple code below, nothing fancy
let server = net.createServer(client => {
// The client can only be used inside of this callback
});
I'm attempting to make a function that would allow for triggering a script in a V8 instance of Node from a node-chakracore instance. [...] The way that I'm wanting to trigger scripts is by having an IPC socket, the server side being Chakra and the client side being V8.
The general rule of thumb is: clients trigger events in servers, not the other way round.
Luckily for you, this is perfectly possible, since Node is a server, and it can send requests to other servers. So you can have the Node-Chakracore instance running in a server role with respect to the actual requests you want to serve, and at the same time act in a client role towards the Node-V8 instance (which is running on a different port) by sending requests there.
You can either start the Node-V8 instance separately by hand, or you can have it be started as a child process by the Node-Chakracore instance (on startup). Either way, the two instances would talk to each other via a socket (HTTP or other, your choice).
A bit more detail, in case it isn't obvious: For the Chakra instance, you'd write a handler that sends a request to the V8 instance, and that second request gets a completion handler that optionally processes its result and hands it off to the first request's completion handler. (So this is all very similar conceptually to processing a database request or similar; the "database" just so happens to be another Node instance.)

Waiting for/receiving an external web service request in cucumberjs

I think I just need a pointer in the right direction.
I am testing an application server (through its REST API). I'm using cucumberjs
When I invoke a specific method, some time later the application will send a notification to a URL. The URL is configurable, but only in the app settings - i.e. I can't give a callback URL for each invocation.
In my test step, I need to wait for (and receive) that notification, and extract some data from the request body to use in later steps.
How do I go about this? I guess I could set up a web server for each scenario, pass it a reference to my World object, and have it update something there with the details of the notification (it's OK to configure the app settings to point at my testing app).
But how do I wait (with a timeout) for the notification to be received?
(Ideas, and pointers to doc I should have found, suggestions for node.js packages etc. all welcomed)
I am performing similar types of testing where I need to trigger something in a could-based system and then wait for some behavior to occur. In order to achieve this, I'm using the promise-retry NPM package to perform polling (when needed) and then configuring the default timeouts in CucumberJS to be higher than normal (usually around 60 seconds). I also use config to make all these timing configurations easier to manage.
As far as how you're interacting with your system, it depends on what type of system it is. If you're using Azure, AWS, or Firebase there are API clients that they provide.
If you need to poll, I would recommend a promise-based HTTP client like flashheart, axios, or superagent. CucumberJS itself does not provide these capabilities, but it's easy to bring in other modules for CucumberJS to integrate with.
If you want to receive notifications directly, you could use some sort of cloud-based event hub like Azure Service Bus. Alternatively, if your tests are running on the same network as the system you could easily run an Express HTTP server within CucumberJS. As you receive messages, push them onto an array and then have a step definition to assert that the array contains the expected message.

Is it possible to force Socket.io to use its Ajax fallback?

I'm making an Ionic app that needs real time data (a few seconds delay is OK) and I want to use Ajax (with or without long polling) because I may not have the resources to use websockets. However, I want to make it easily upgradable to websockets. Since Socket.io uses Ajax as a fallback, is there a way to force it to use Ajax? This way, I can easily upgrade to websockets if I have the resources.
P.s. just confirming, a server can usually support more clients using Ajax than websockets right? (assume 5-10 seconds per Ajax request).
Assuming your backend is node.js, var socket = require('socket.io')(); and your socket.io version is 0.x:
socket.set('transports', [
'xhr-polling'
]);
Otherwise the new syntax for socket.io 1.x is:
var socket = require('socket.io')({
'transports': ['xhr-polling']
});

NodeJS: Redirect Client to Another Worker/Instance

I got multiple instances of the same Nodejs application running on a website. I use websocket.
Let's say that the client is connected to the instance #1. I want to transfer him to instance #2.
What I'm doing is that Instance #1 sends the data to Redis DB, then instance #2 gets it. But how do I redirect the client to the other instance?
You can try socket.io,it can solve your problem
use npm install socket.io to install this package , and go to http://socket.io/ to learn document
I am pretty sure you cannot pass an open socket from one node process to another.
If you have a way to access a specific instance from outside (using a different port, ip or dns) you may either route socket connection at handshake (using http redirect) or after connection was established using a special event that the client will use to disconnect and reconnect to the other instance.
You will need to disconnect from #1 instance and connect with #2 instance. There is no way you can do soft hand-off though you might be able to emulate it in some manner by writing a wrapper on client side, which will disconnect from #1 and reconnect to #2.
If you use load balancer, just disconnect from client or server side (assuming you take #1 instance out of rotation) and have client re-connect.
there is front-end and server-end in socket.io ,
between two end,you should regist events,in server-end ,you should regist client-event: emit server-event which regist in client-end ,and use broadcast.emit to send message to other instances,
also in client-end,you should regist the server-event:emit the client-event which have callback function

Categories