Unable to deploy an application on Heroku due to some port problems - node.js

I've been trying to deploy a repository https://github.com/evelynhathaway/triton-poll to heroku, but since I am fairly new to NodeJs, I am unable to detect the problem. But I guess it's due to the port because heroku doesn't use static ports.
Any help would be appreciated.
Thank You in advance.

I looked at the fork and you did a couple of mistakes. I don't have the time to fix, test and get it to run but I can show you how I solved it before.
All the relevant code changes can be found in this commit (different project):
https://github.com/vegeta897/d-zone/commit/63730fd7f44d2716a31fcae55990d83c84d5ffea
The project is divided into a client and server part.
You can see here, https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/script/websock.js#L16, how I combined server and client into one. This only works because the static client files are served via http/https and the server uses websocket, no http ws/wss
When you publish a server on Heroku you need to bind to their dynamic port. However when you want to access the web server you do not specify a port. The hostname is automatically translated into an ip-address + port combo. I did this here: https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/web/main.js#L44 When deployed on Heroku the socketURL does not contain a port number.
Finally you bind to the server. I did it here https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/script/websock.js#L55 and here https://github.com/vegeta897/d-zone/blob/63730fd7f44d2716a31fcae55990d83c84d5ffea/socket-config.js#L30
You also have to make sure that your clients files are built properly and served.

Related

Cannot establish connection using port number explicitly

I am new to servers and networking so pardon my ignorance.
I have a Heroku application running a NodeJS server. I am using console.log() to output the port its using to the console. But when i use the port to try to perform a GET request from my browser it keep loading forever. My request is something this:
https://example.herokuapp.com:28222/getHighest
When i remove the port number, it works perfectly:
https://example.herokuapp.com/getHighest
I am ultimately trying to perform GET and POST request from a C application. The HTTP library i am using seemingly requires a port for a connection. I am using this library: GitHub. It works perfectly when i run it locally with localhost:8080/getHighest but not when i use my heroku app.
As suggested by #tadman using the default https port 443 solved the issue.

How can I access Heroku NodeJS server externally?

I have a websocket server running via NodeJS, and have deployed it to Heroku. There are two separate web applications I wrote that communicate with the websocket server. On localhost, I simply run the node server, load up the applications in a web browser, and all works fine. In Heroku, however, I can't seem to get anything to connect to the url ws://url:port - it just returns an err request timeout.
I don't want to deploy my html using Express per their example. Maybe I could, but this is a test case where some web applications get to be pretty huge.
Is my issue that I have to use wss instead of ws?
Or backing up further, is this a good use case for Heroku or is there something else I should use?
The easiest description of what I'm trying to do: Two websites hosted somewhere that isn't Heroku both communicating with a NodeJS-based websocket server hosted on Heroku.
Thanks for your help!
Alright, I feel pretty dumb because this is about as easy an answer as it gets. For anyone else trying to do the same thing, you don't need to specify the port (Heroku port is for internal use only, and the heroku url you are provided is on port 80). Simply connect using wss (e.g. wss://your-app.herokuapp.com)

Run socket.io chat app in a website that's already running on wampserver

I have recently made this socket.io chat app with the help of online videos. But the problem is I modified the code and now I want to integrate it in a bigger website like a social network.
The problem, apparently, is the code can't find the socket.io.js file even though it is in it. I know you would usually run a server from gitbash, but it is already running one so why run gitbash as well?
The exact error is:
GET http://localhost/socket.io/socket.io.js ERR: NET ABORTED
This is causing all the trouble.
Also, any opinions on doing all of this in php. I can do it in php, it's easy.
You're trying to load the file from
http://localhost/socket.io/socket.io.js
HTTP requests directly to localhost will go to localhost port 80 by default, which you probably don't have a server running on.
Change the source of the js file to the port that your app is running on, should look like this:
http://localhost:3000/socket.io/socket.io.js

Why does ec2 instance not display my website? Using nodejs

I am running a m1.microinstance of aws, using CentOS. I downloaded Yeoman, git, npm and all of the dependencies are present. I am trying to run a MEAN stack on this server, so, mongo, express, angular and node. However, when I visit my public DNS, my site gives me this error: "Oops! Google Chrome could not connect to ec2-54-191-0-63.us-west-2.compute.amazonaws.com". On my admin control panel, I see my instance status, and it says it is running. I understand that if I had used apache, the page that displays is in the /var/www/html directory.. So, how do I get a directory similar to apaches, to display my html files, or whatever I would like the public to see? I have my security groups configured, for inbound, to listen to SSH port 22, for everyone, as well as HTTP port 80, for everyone.
Yeoman set up a nice app folder for me, but for some reason it does not display. I thought maybe I was missing a server.js, but that does not seem to have fixed anything when I added it. Any advice? Thanks!
Make sure you are matching the port all the way through - your browsers URL:PORT, the EC2 routing rules and your NodeJS settings. It looks like you might be listening to a port higher than 80 on the server.
As you mentioned in your comment, if you want to listen on a port below 1024 you will need to run the command as a privileged user.
I didn't run node as root on my AWS server, so it was not setting up my nicely built app that Yeoman made for me.
http://www.stackoverflow.com/questions/9164915 was where I realized my mistake. I am new to linux OS soo, I am learning. :)

Deploying a TCP server to Heroku

I have a TCP server coded in node.js. I'd like to put it up on Heroku because it's a free service and I don't need anything more than what their free plan offers.
Now, I know very little about the inner workings of Heroku and I'm pretty new to the whole thing so I have a few questions.
Firstly, is it even possible to deploy a TCP (non-web) server? I've read that Heroku doesn't like node.js's net because it doesn't support websockets and that I should use socket.io.
So I've switched my server to socket.io. I think. Because my code more or less looks the same. I've done this as well: https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku
What do I put in my Procfile instead of "web"? Also, when I tried to deploy what I currently have, the logs said that my application failed to bind to $PORT. What's $PORT? And how do I change it to the port I want?
In fact, if I don't change it, how do I know what it is so my application can connect to this server?
The free tier of Heroku does not support TCP server. Here is the reason.
To save costs and offer free services, Heroku hosts multiple free-tier apps on the same machine. These apps, one of them being yours, share the same IP address. The apps are assigned to different ports.
However, as you probably noticed, when you access your app in a browser, the port is always 80. Hence, to know which app an incoming HTTP request is looking for, the server must be looking into the HTTP headers. (For example, using HOST to find out the app name, then resolve the app name to an internal port number.)
Finally, Heroku decided to hide away the internal ports from the internet. This, along with the fact that TCP connections don't have a HOST field, makes it impossible to host a TCP server with Heroku.
To work around this, use WebSocket.
Appendix: the Research
Testing was done with a free-tier Heroku app, in March 2020.
If you make up a non-existing app name (e.g. https://hr.herokuapp.com), Heroku responds with a page saying "There's nothing here, yet."
If you first manually nslookup an existing app (e.g. https://world-of-blogs.herokuapp.com), then try to use the IP address to access the app, Heroku also responds with a page saying "There's nothing here, yet."
Heroku doesn't support a generic TCP server but you should be able to get the functionality you want with socket.io.
You need to put web in your Procfile. That's what lets Heroku bind an external connection to port 80 to the local port your web traffic will arrive you. You find that port by looking at the environment variable $PORT. More info, with examples, is here: https://devcenter.heroku.com/articles/nodejs

Resources