SocketIO is not working in Heroku Environment [Works in Localhost] - javascript

I developed chat program with NodeJS and SocketIO. Then I deployed to Heroku Server and I found that SocketIO is not working in Heroku.
I already checked the log in Heroku but there are nothing. But it is weird that it is working in localhost but not in Heroku environment. I think there are some problem with PORT Setting.
[Server Side Code]
/* Socket IO Settings */
const server = require('http').Server(express);
const io = require('socket.io')(server);
let port = process.env.PORT || 3001;
server.listen(port, function () {
console.log(`SocketIO Listening on port : ${port}`)
});
[Client Side Code]
/* Chat Functions */
var socket = io.connect(`${window.location.hostname}:3001`);
At first time I just use var socket = io.connect('http://localhost:3001'); code and it works well in Localhost Env. But after I deployed to heroku I add Heroku Variable to use it in production.
[PORT Setting]
NODEJS Server : 3000
SOCKET IO : 3001
Heroku process.env.PORT : 3001 (But When I check Heroku console it changes everytime when I reload the dynos and in Logs said me that the ports are already in use.)
I already read Heroku Document but it seems that I create NodeJS App with Express Generator I think it should be different solution. I already tried heroku document but it's not working

Set your server ip in server.listen function
var ip='127.0.0.1';
var port ='3001' ;
server.listen(ip,port, function() {console.log('server stsrt in ip: ' +ip+' and port: ' +port);}) ;

Related

Correct ports for node server-client application with Heroku

Well, I'm near to finish my starting app... but port issue is hard...
My server.js has this
const port = process.env.PORT;
const express = require('express'), http = require('http');
const app = express();
const server = app.listen(port);
const io = require('socket.io').listen(server);
And now the issue. What should I do in the client?
var socket = io.connect('https://myweb.herokuapp.com:3000/');
var socket = io.connect('https://localhost:3000/');
I tried those two... the second one, localhost (but with 8080) works in local... but displays the error
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=M9zP_yz net::ERR_CONNECTION_RESET
I read that the client is picking the CLIENT PC localhost and that fails, that has to pick the SERVER PC route... so I tried the first option, the application URL... because I suppose I don't have to use the server IP (I also don't have it). But nothing works...
Try using port 80, that should be the default port accessible outside heroku dynos: https://myweb.herokuapp.com:80/. If it does not work, try connecting without the port. Use the URL only: https://myweb.herokuapp.com

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'));
});

Ubuntu open/unblock ports for nodejs

I'm trying to listen port 8080 for socket.io but I get error:
http://localhost:8080/socket.io/?EIO=3&transport=polling&t=Lai1FQh net::ERR_CONNECTION_REFUSED
I'm using ubuntu virtual machine from digitalocean and first when I ssh login I get message:
The "ufw" firewall is enabled. All ports except for 22, 80, and 443 are BLOCKED.
I'm beginner with this but doesn't that mean that port 8080 is blocked.
How do I fix this and what I need to do
Here is my code for server:
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
server.listen(8080, function(){
console.log("Listening");
})
In my website i just connect to that port like this:
<script src="node_modules/socket.io-client/dist/socket.io.js"></script>
<script>
var socket = io.connect( 'http://localhost:8080' );
...
<script>
The error message you get means that ufw (it's a firewall) is blocking all ports except those 3.
To enable port 8080:
sudo ufw allow 8080
Then reboot your server.
(As seen from the guide available on your hosting provider website)
You probably have another instance of your program running, or some other program that uses that port. Run:
ps aux
in your shell to see what processes are running and kill those that shouldn't be running. You can also run lsof or netstat to see what is listening on which port. See the manuals for those commands.
If you can open 8081 or something like that but not 8080 then it means that something is already listening on port 8080. You can verify it with trying to connect to that port with curl, wget, netcat or by using nmap. See their man pages for details.

What HTTP address can I send requests to from when hosting on Heroku? (client to server)

This sounds really dumb, but...
I have a Node.js Express server that used to be completely functioning on my localhost. I recently deployed it on Heroku and made some changes so my actual index.js listens to process.env.PORT instead of localhost. it listens to process.env.PORT fine through that, but I have a page rendered in React.js called app.js stored within a directory (./js/app.js) that makes HTTP requests when buttons are clicked.
However, whenever I try accessing process.env.PORT from within the app.js to make these requests, process is undefined, so everything falls apart. When working on it locally, I would just send the requests to http://localhost:3000/database, but now I have to change that since it's deployed on Heroku now.
How can I communicate between my client (app.js) and my server (index.js)?
I know this is something super simple and I've tried searching for it for the past couple of hours, but I just don't know how to phrase it.
Thank you!
Try to define your address and port in your app.js file as a heroku server address:
var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port; // put your adress here :)
console.log('Example app listening at http://%s:%s', host, port);
});
I am there if you get another pb :)

process.env.PORT prints weird string

When running console.log("Express server listening on port " + port); in node.js on a deployed Azure website, I get the following string
Express server listening on port \\.\pipe\d9797e42-9e1f-421c-91b9-3d86496eaeb8
What is this and how can I determine what port my site is really running on?
Should be running in port 80.
Seems like an internal instruction from the server

Categories