Communicating over socket.io with a heroku app - javascript

I have two applications that need to communicate with each other. One is a node app deployed on heroku. The other is a node app deployed on a raspberry pi3. I need the raspberry pi3 to send the heroku app data using socket.io
On the heroku app I have
http.listen(process.env.PORT, function() {
console.log("listening on *:" + process.env.PORT);
});
Which listens on the port Heroku assigns.
On the raspberry pi app I have :
let socket = io(`http://appName.herokuapp.com:7256`)
I logged out the port number from heroku when it connected and it was 7256. However, I am unable to communicate with the application on Heroku. There are not any errors when I am trying to emit from the pi. But the Heroku app isn't receiving anything. Is there anything else I need to do?

Use port 80 (or no port at all, to default to 80).
The PORT env var is an internal port - it's the port within the Heroku container that your app should listen on.
Externally, Heroku's routers expose your app online at the normal ports: 80 and 443 (http and https).

Related

What is the significance, if any of port 9229, and can it be changed?

By default, I was taught, when I run a local Node server to run it on port 3000.
After reading a tutorial on the Chrome Node debugger which is set to inspect port 9229, I switched to this port.
Here is a similar Q / A on port 3000.
Here is the tutorial.
Can I switch the default to 3000 some how?
This way it will match my Express local port:
app.set('port', (process.env.PORT || 3000));
Or would it be more prudent to change my local express server to 9229?
I know this is a super old question in SO time but if anyone else comes here for whatever reason, here's the skinny.
TLDR; Port 3000 is for your application, Port 9229 is for the NodeJS debugger.
Port 9229 is the default NodeJS debugging port. It allows you to attach a debugging tool like Chrome's Inspector or WebStorm's Remote Debug to a Node process that is running with a special flag (node --inspect server.js in Node v8+ I think, no idea what it was before that). This has nothing to do with what port your listing to in your HTTP application.
Port 3000 is the "recommended" port to run HTTP applications like Express, Koa, Hapi, etc. I have no idea when it was decided that port 3000 was the goto but here we are.
If you want to read more about NodeJS debugging here's a link.

GCP deployment port issue for a node js project

I have deployed one node js project in which a http server is created at port 8080 and listening at 8080 and a service url is generated.But inside http.createServer i am creating another server for a nlp engine which is listening at port 8081.So using the service url generated after deployment i am able to hit server running at 8080,but how to access the nlp engine server running at 8081 using the same service url with out using router?ordo gcp doesnt allow creation of two server listening at two ports with the same service url?
Can you try http://hostname:8081? what does it say?
If the port 8081 is open, then ideally you should be able to access that NPL engine, else you may need to get the port open in GCP.

how to listen on any port on heroku when a web port 80 is already in use?

I am new to heroku and i have deployed a meteorjs application on heroku. My Meteorjs application is a webapplication, so after the build is done it runs on heroku on port 80. But simultaneously i also want to listen on a port eg:4000 or any so that i can catch my webhook events fired by any third party i want to listen to. On my local machine it runs perfectly with webapp running on a port and listener running on another, but on heroku after i deploy its just the webapp which runs and listener doesn't listen. Can anybody help me or guide me. Any new solutions are also welcome..
In my server/main.js i have made a bot instance of facebook-bot-messenger and below is the code to start the listener
bot.listen(4000);
In my client/main.html i have the html code which is just hello world
On local machine when i visit http://localhost:3000 i can see helloworld and app is also listening on port 4000.
On heroku when i visit https://appname.herokuapp.com i see helloworld . In this case the port is 80 which is fine, but the bot is not listening as any post calls i make on https://appname.heroku.com on port 4000 doesn't respond.
Heroku assigns the port for you, so you can't set your own. You will have to read in the port number from the PORT environment variable. In Node this would be process.env.PORT
You can also use || for as and OR. For example in the below case, it will read in the port number from the environment variable while on Heroku, but locally it will default to port 4000.
const app = express();
app.set('port', (process.env.PORT || 4000));
//Start Server
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});

I don't have to specify a port for heroku for my socket.io app?

I've successfully made a test chat app and I've gotten a node js server with socket.io running on heroku. On my local computer I have to specify the port number of localhost on the client side to the port that the server has set up. However, when I run my server code on heroku. Removing the server
I'm using the process.env.PORT variable since heroku sets that up:
var port = process.env.PORT || 3000;
http.listen(port, function(){
console.log('listening on *:' + port);
});
Naturally I find the port number that the app is running on place it in the url
var socket = io('https://xxxx.herokuapp.com:1111');
However this gives me an "net::ERR_CONNECTION_REFUSED".
I got it to work by removing the port nubmer after the url (in this example :1111). I'm wondering why this is working since most of the tutorials and articles online have it specifying the port and why my local computer needs the port to work as well.
When you connect to your https://xxxx.herokuapp.com subdomain on heroku on port 443 (which is the port that is used for an https connection when no port is specified), Heroku is probably using a proxy or router to route that incoming connection to the particular port that your node.js server is listening to. In the Heroku infrastructure, they know what actual internal host your server is running on and what actual port number it is running on so they can map a default port request on your subdomain to the actual port/host.
This is done so that browsers can connect to your subdomain directly on the default port without having to know the particulars of your node server installation and so that Heroku can auto-manage your server and likely share hardware with other customers. You are each running on a different port, but sharing the same machine. The ports are managed entirely by Heroku and this is one way that they are able to put multiple customers on the same hardware without each having to specify a custom port in the browser URL (which would be a non-starter for most customers).
So, Heroku is hosting some sort of proxy for your sub-domain that is listening to the default https port. Thus, you don't have to specify the port in the URL. Internally, they route that connection to your actual port on your actual server.
When running on your desktop, there is no such proxy to do this for you so you have to make sure client and server port numbers match.

Trouble running socket.io in ports other than 80

I'm running express.js on port 80, so socket.io has to go on other port. The problem is that, then, I have to load socket.io on the client with:
<script src="http://my_domain:8080/socket.io/socket.io.js"></script>
Which is horrible because I have to change my_domain everytime I switch from local machine to host.
I'm running express.js on port 80, so socket.io has to go on other port.
Wrong.
You can run Socket.io and Express on the same port; just pass same same http.Server instance to both of them.

Categories