How get second port from Heroku for websocket - node.js

I'm using Heroku to deploy my nodejs game.
Everything works fine but the websockets on my game won't work when I deploy it to Heroku.
Heroku gives me 1 available port (in the var port).
Is there a way to get a second port where my socket can listen to?
If I try to set the socket to the same port as the app.listen, it tells me the port is already in use.
var port = process.env.PORT || 8080;
app.listen(port);
socket = io.listen(8000);

Related

How do I set node app to run on a static port number on heroku?

my node app runs on port 8083 locally. when I push to heroku, how can I configure the service to run on the same port?
You cannot do it. Your code should listen on the port that you have in the PORT environment variable passed to you by the Heroku server that you can access as process.env.PORT and Heroku will listen on the outside on port 80 for HTTP and 443 for HTTPS.
See the docs:
https://devcenter.heroku.com/articles/runtime-principles
In particular:
https://devcenter.heroku.com/articles/dynos#web-dynos
https://devcenter.heroku.com/articles/http-routing
https://devcenter.heroku.com/articles/ssl-endpoint
Correct example:
// Get the port:
const PORT = process.env.PORT || 3000;
// Listen on the port:
app.listen(PORT, () => console.log('Listening on', PORT));
The default (3000 in this example) is for situations when you run it outside of Heroku (like for testing). When it is run on Heroku it should always listen on the port provided by Heroku. If it listens on some other port then Heroku will not proxy the traffic to your app correctly.

Openshift - port to use on deployment

I have the following start.js file:
var express = require('express');
var app = express();
app.use(express.static('static'));
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
In my NodeJs application on Openshift. However, when I run rhc tail-a app-name
I can see that there is an error of :
Error: listen EADDRINUSE :::8080
I've tried 80 and 443, and received those errors:
Error: listen EACCESS 0.0.0.0:443
Or 80
Which port should I use as default on my app?
Thanks!
Use Nginx,
Nginx (pronounced "engine x") is a web server. It can act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer and an HTTP cache.
It isn't good practice to run your application with root privileges or directly run your application on port 80 and your port 8080 is in use. Try different port and use reverse proxy.
But if you want to run on port 80 or 443, run your application with root privileges.

Node.js chat server running on "cloud9-ide" is listening but not able to connect using client

Chat server is listening to the port, below snippet is from console log.
Your code is running at https://mynodetest-someone.c9.io.
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!
debugger listening on port 15454
info - socket.io started
Chat server listening at 0.0.0.0:4000
When trying to connect through browser: eg: "localhost:4000", connection is refused(Error code: ERR_CONNECTION_REFUSED)
Can some one help how to fix this issue?.
Note:
I have tried listening on "127.0.0.1" and given original IP address in the network.
You can access the server using the URL that cloud9 provides for that specific server/project.
Use process.env.PORT as the port and process.env.IP as the host for you application and access your app using your project name - username. Should be something like https://mynodetest-someone.c9.io.
From the c9 page:
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!

Node.js socket app on openshift

I'm trying to deploy a simple node.js socket app on OpenShift.
First I tried setting up the listener as:
var server = net.createServer(newSocket); //newSocket is a listener method
var port = 8888;
server.listen(port);
and this causes:
Error: listen EACCES
Then I researched a bit and learned that you need to listen using OPENSHIFT_NODEJS properties and set the listener like this:
var server = net.createServer(newSocket);
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8888;
server.listen(port, ipaddr);
Now the app is started at: 127.6.253.1:8080 - however when I try to telnet it using my OpenShift app url and 8080 I get server timeout.
If you have experience with the similar situation let me know.
The code of the app I'm trying to make it work on OpenShift is at https://github.com/denimf/NodeChat
The internal port for the OpenShift app is 8080, but it is exposed externally on port 80 at the URL specified in your control panel. You can also see the app URL in the console by doing:
echo $OPENSHIFT_APP_DNS
Most of the node.js web hosting services don't support socket listener. I solved my problem by hosting the Node app on a dedicated virtual machine.

Node.js + Express only work on port 3000 on test server

On Local Test it work. on different port (3001, 8080)
But on Test Server (Azure)
I run 2 instance of Node App on same machine
$ node api1/index.js (on port 3000)
$ node api2/index.js (on port 3001)
and
$ node api1/index.js (on port 3001)
$ node api2/index.js (on port 3000)
But it only works on port 3000.
How do I set different port in Express?
Now, I've changed at app.listen(3001) on index.js and it doesn't work.
Often cloud platforms set an environment variable that contains the port they want you to stick your app on. I don't have any experience with Azure... See the answer here: How to run a node.js server on Azure?
Specifically:
var port = process.env.port
Most cloud providers in my experience don't let you play on other ports. You can always specify a localhost port too, though by doing this:
var port = process.env.port || 3001 //(or whatever)
app.listen(port);
this way if process.env.port is undefined (which it will be in your dev environment) you fallback to 3001.
Make sense?

Resources