CONTEXT
I am trying to build a new web app in NodeJS. This webapp uses two main components.
1) Code that I am writing within NodeJS - AKA my own logic flow.
2) A 3rd party open source NodeJS app - EtherCalc - that uses both HTTP & Socket.io
EtherCalc is a completely standalone web application on its own. It is a spreadsheet-in-a-browser+server meant to be used as a standalone app. Of importance, it has its own namespace (as in it has various pathnames that route to different functions).
My app and EtherCalc each run on their own ports independently of each other.
For simplicity's sake, let the domain name be localhost
.
CHALLENGE I AIMED TO SOLVE
My application will be using both the spreadsheet capabilities of EtherCalc, as well as the non-spreadsheet-related logic flow of my own code. Users will have access to both interfaces. However, I want this all to appear to come from one URL/port - I don't want non-programmers to be specifying different ports to access different functionality.
The easiest way to tackle this for me was to create a namespace for EtherCalc within my app. Anything path that starts with /ethercalc/
automatically gets forwarded to the port that EtherCalc is running on (with the ethercalc/
removed from the request URL before it is forwarded). Anything that doesn't start with that stays within the standard server logic flow.
I'm using node-http-proxy
to make this happen, using the following code:
proxy.proxyRequest(req,res,{
host:'localhost',
port:8000
});
PROBLEM I HAVE COME ACROSS
This seems to function fine initially - the spreadsheet interface loads when I go to any /ethercalc/
url. However, EtherCalc uses either WebSockets or JSON Polling in order to keep the server & the browser on the same page. This doesn't seem to work properly. For some reason, it takes a good 10 seconds to actually get that connection going. This is especially problematic if I've worked on a spreadsheet, then load it again later - there's a 10 second window before I actually see data I've put in beforehand.
After having this issue was to remove the url-changing functionality. Now my app simply forwards the requests to the port EtherCalc is running on. However, the problem remains. Connecting directly to the port that EtherCalc is on removes the problem.
Rewriting the code to use node-http-proxy's httpProxy.createServer()
code didn't make a difference either - same exact outcome...
What's bizarre is that when connecting to my app port (the one that has a proxy/forwarding system in place), AFTER the lengthy wait period, it functions just fine - everything is completely synced up in real time.
Does anybody have any idea what it is that's going on?
TL;DR
- I have a web app in NodeJS facing HTTP port 80.
- I have a 3rd party web app in NodeJS using Socket.IO (EtherCalc) running on the same server on a non-public port.
- I want to serve webpages from my own webapp AND webpages from EtherCalc to the same domain name on port 80.
- I have a proxy (
node-http-proxy
) running on my webapp to make this possible. - For some reason when I do this, the handshake that takes place between the browser and the server for EtherCalc (now running through a proxy) takes FOREVER. (or rather 10 seconds, AKA unacceptable for a consumer facing webpage)
- I have no idea why this happens.
Any clue/hints/suggestions/leads? Socket.IO is completely new to me - I have used Node for a long time (2 years), but it's been entirely in HTTP world. Proxies as well are new to me (this is my first time using one - using it for the namespace/port issues). As such, I really have no idea where to begin looking.