WebSocket on Heroku ERR_CONNECTION_REFUSED - javascript

I am trying to run a node js app on Heroku using WebSockets. However, I am not able to resolve this error (As seen in conosle of Chrome browser)
WebSocket connection to 'wss://myappname.herokuapp.com:27225/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I am using 'wss' since Heroku runs on HTTPS.
My client side code is :
$.get("https://myappname.herokuapp.com/port",function(data){
port = data;
console.log(data);
host = 'wss://myappname.herokuapp.com:' + port + '/';
ws = new WebSocket(host);
});
My server side code is :
var WebSocketServer = require("ws").Server
var fs = require('fs');
var express = require('express');
var app = express();
var http = require('http');
var port = process.env.PORT || 5000;
var request = require('request');
var server = http.createServer(app);
var serverOnPort = server.listen(port);
console.log("Server listening on port ",port);
var wss = new WebSocketServer({server: serverOnPort});
console.log("websocket server created");
Any help would be appreciated.
Thank You.

So it seems like I was trying to over-do it with the port number. Just using the host as wss://myappname.herokuapp.com/ works well.

I also found this problem. It seems Heroku will automatically route port number. It does's allow to specify port number in url. In my chrome, it show ERR_CONNECTION_RESET. This also happen with XMLHttpRequest. Port number still need when you test with localhost or another server which is not Heroku.

Related

Socket.IO ERR_CONNECTION_REFUSED

I am getting this error:
WebSocket connection to 'ws://localhost:8000/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I have looked for this error but it seems for me that my configuration of the sockets is well and I do not think is for the warning of electron.
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var JSONTCPSOCKET = require('json-tcp-socket');
var JSONTCPSOCKET = new JSONTCPSOCKET({tls: false});
require("./rabbit")(io, JSONTCPSOCKET);
var socket = io('http://localhost:8000',{transports: ['websocket',
'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling']});
Any idea?
Thanks mates!!
It was a silly mistake.
The file that contains the server that listen in that port is in my case server.js.
When you run it with node, the start file is server.js but when you run it with electron the start file is main.js and I was never running server.js when I executed with electron, so I was not listening in that port.

Socket.IO server only accessible locally

I have a Socket.IO server (Node.js, NPM) that works fine, but the problem now is that it only works in LAN.
I'm using Socket.IO 2.1.1.
My IP is my IPv4 address, and the port is 80. Here is my server.js code.
var express = require('express');
//var http = require('http');
var url = require('url');
var socket = require('socket.io');
var crypto = require('crypto');
var fs = require('fs');
//App setup
var app = express();
var server = app.listen(80, function() {
console.log('listening to request on port 80! Yay!');
});
//Static files
app.use(express.static('public'));
io.on('connection', function(socket) {
//stuff to do on connection
}
and for the client
var socket = io.connect('http://192.168.254.7:80', {transports:['websocket']});
and I also included
<script language="javascript" src="http://<ipv4>:80/socket.io/socket.io.js"></script>
where IPv4 is the IP address i'm using.
How do I make it public? (not only locally / LAN) I've tried changing
io.connect('http://192.168.254.7:80', {transports:['websocket']});
to
io.connect();
I've even changed the port from 3000 to 8080, and to 80.
I let my friends try to access the website, and it always shows
net::ERR_CONNECTION_TIMED_OUT. Does it even load to the other's device?
Again, how do I make this public? Thanks.

Cannot set an IP address on Express JS app

Trying to test an endpoint in express But keep getting 404 error.
var express = require("express")
var app = express()
//var http = require('http').Server(app)
app.get('/', function(req,res){
res.send('ok from end point')
})
var port = process.env.PORT|| 8080
var localhost = 'someLocalHost.med.gov'
console.log({'localhost':localhost,
'post':port})
//
app.listen(port,localhost,function(err){
if (err){
console.log('err')
}
else {
console.log('Listening')
}
})
When I go to
http://someLocalHost.med.gov:8080/
I get a 404 error
Localhost refers to 127.0.0.1. You can't just launch a server on any address that you want. If you're wanting to override localhost you can look into modifying your HOSTS file locally to setup an alias for localhost.
So I ended up http:// IP_ADDRESS:8080 and that took care of it.

NodeJS disconnecting when attempt MongoDB connection

In my app.js file, I have the following code
var express = require('express');
var app = express();
var port = 8080;
var util = require('util');
var router = require('./base/js/routes.js');
//==================================================================
app.use('/', router);
// start the server
app.listen(port, function(request, response) {
console.log('Port 8080: Server Begins');
});
//==================================================================
var ipaddress = '123.456.789';
//==================================================================
var mongoose = require('mongoose');
var mongoURI = "mongodb://"+ ipaddress +":27017/test";
var MongoDB = mongoose.connect(mongoURI);
MongoDB.on('error', function(err) {
console.log(err.message);
});
MongoDB.once('open', function() {
console.log("mongodb connection open");
});
//==================================================================
The line var MongoDB = mongoose.connect(mongoURI);
is causing nodeJS not to work. I do not know why. NodeJS is on port 8080 and MongoDB is on port 27017.
I am fairly certain I installed mongodb package (and opened the port correctly). I just do not understand why nodeJS doesnt work when i include that connection line.
Side Note: Also I have the package forever installed: forever start -c nodemon app.js for nodeJS. If that is any relevance.
You are using wrong IP address format.
First try to connect with your local mongoDB instance if it work then you to check the IP address your trying to connect is correct or not.
Add the correct error message if problem still remain same.
change your mongod.conf file from /etc folder
In mongod.conf you need to change bindIp
If connection is local then set bindIp as
bindIp = 127.0.0.1
and if you want to use remote database then change bindIp as
bindIp = 0.0.0.0
then restart mongo service
hope this helps...

Connecting python socketio_client to nodejs server facing 400 error code

I tried to connect python client to node.js server, and experience HTTP 400 errors.
node.js server code:
var socket = require('socket.io');
var express = require('express')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 8080);
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socket.listen(server);
io.sockets.on('connection', function () {
console.log('hello world im a socket');
});
python client code:
from socketIO_client import SocketIO
def on_response(*args):
print 'on_response', args
import logging;
logging.basicConfig(level=logging.DEBUG)
socketIO = SocketIO('localhost', 8080)
socketIO.on('news', on_response)
socketIO.wait(seconds=1)
when i run client.py after starting the server getting the following error:
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
DEBUG:requests.packages.urllib3.connectionpool:"GET /socket.io/1/ HTTP/1.1" 400 None
If I create a javascript client, then it works well. Could someone help me resolve this issue please?

Categories