ECONNREFUSED on socket connect in Node app on openshift servers, works on development machine.
Hi, I am trying to write simple app that needs to make a outgoing socket connection from my server.js ( that came with the pre-installed template). In my express routes i have something like
self.createRoutes = function() {
self.routes = { };
self.routes['/asciimo'] = function(req, res) {
var link = "http://i.imgur.com/kmbjB.png";
res.send("<html><body><img src='" + link + "' /></body></html>");
};
self.routes['/mycfg'] = function(req, res) {
var serviceSocket = new net.Socket();
serviceSocket.connect({port: 443, host:"www.google.com",localAddress:self.ipaddress}, function() {
console.log("connected!!");
});
serviceSocket.on("error", function (e) {
console.log("Could not connect to service " + e);
});
}
}
The self.address is t process.env.OPENSHIFT_NODEJS_IP which is 127.4.217.129 in my case.
I tried the code on my development machine it works fine. But fails with ECONNREFUSED on openshift servers. Any help would be appreciated. Thanks for your time.
The problem was Node version 0.6.25. I created a new app with node verison 0.10 and the same code works fine.
Try something like this instead
var host = process.env.OPENSHIFT_NODEJS_IP;
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({host: host, port: port});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
ws.send(message);
});
ws.send('something');
});
EDIT
My apologies I overlooked that you were making an external connection. After reproducing it locally I was able to connect by removing the localAddress on your Sockect.connect. After looking at the documentation and source a bit this makes sense. Making a connection to google and binding to a local ip won't work due to google not knowing this local ip.
Related
I create app with nodejs socket io. It works clearly at localhost (port: 3000). But when i deploy it to my server in there i can run my app on 3000 port but client side throw timeout. How can i solve it?
var fs = require('fs');
var https = require('https');
var options = {
key: fs.readFileSync('ssl.my-key.key'),
cert: fs.readFileSync('ssl.my-cert.crt')
};
var server = https.createServer(options);
var io = require('socket.io').listen(server);
var port = 3000;
const database = require('./Database');
io.on('connection', (socket) => {
socket.on('message', async (msg) => {
// I do some action here.
});
socket.on('disconnect', (msg) => {
// some action in here too
});
});
server.listen(port, () => {
console.log('listening on *:' + port);
});
It seems like your issue is with port forwarding.
In order for your server to be publicly accessed, it needs to have all ports forwarded appropriately. Locally and on the router.
Check this link to learn more about how to port forward on linux: https://linuxacademy.com/guide/11630-internal-port-forwarding-on-linux-using-the-firewall/
And this to learn more about router port forwarding, but this will really depend on your router.
https://www.noip.com/support/knowledgebase/general-port-forwarding-guide/
However, I don't recommend you to take care of hosting on your own machine(s). I
suggest you use Heroku, you can op in for their free servers, you don't need to pay.
More about heroku and NodeJS: https://linuxacademy.com/guide/11630-internal-port-forwarding-on-linux-using-the-firewall/
let we debug your node js app.
1) add some logs on database connection, http.createserver, also where you have to check if not success then catch exception
2) you should have to open port on centOs before start your node js app
3) you should have test you with domain name or ip address
as per you comment you got connection timeout , you mean node js server trying to connect with port 3000 but node not able to connect and its throws error with connection timeout
also send your sample code of your main index file so we can investigate your problen
thanks.
I have a server hosted at Amazon Web Services. I am using socket.io with nodejs on my website. Following is the code:
Client Side-
function bindSocket(){
iosocket = io.connect('http://ec2-54-190-34-106.us-west-2.compute.amazonaws.com:8080');
iosocket.on('connect', function () {
alert('connected');
iosocket.on('message', function(message) {
//alert(message);
getNotificationData(message);
//document.getElementById("socket_div").innerHTML = message;
});
iosocket.on('disconnect', function() {
//alert('disconnected');
});
});
}
Server side-
var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
//res.end(fs.readFileSync(__dirname + '/socket_test.html'));
res.end();
}).listen(8080, function() {
console.log('Listening at: http://localhost:8080');
});
socketio.listen(server).on('connection', function (socket) {
console.log('new connection');
socket.on('message', function (msg) {
console.log('Message Received: ', msg);
socket.broadcast.emit('message', msg);
});
});
I get the following error message:
polling-xhr.js:261 GET http://ec2-54-190-34-106.us-west-2.compute.amazonaws.com:8080/socket.io/?EIO=3&transport=polling&t=LjIkBx8 net::ERR_CONNECTION_TIMED_OUT
I have been using the same code(with IPs changed) on digitalocean server. However, I migrated to AWS and I'm unable to get it working.
Any help would be highly appreciated.
I figured it out. The port itself had to enabled from the AWS security group. I added the rule and everything worked.
Hope this helps someone else.
You might not enable the socket ports in the AWS network security group. Also, you have to add the same ports in the ufw (firewall) list.
Open the inbound rules and add the custom port to the list.
Type the following command in the terminal to add the custom port in the ufw list.
sudo ufw allow 5005/tcp
NB: the port (5005 in the example) may vary according to your implementation.
I am trying to set up a simple websocket server that communicates between a Flash AS3 client and a Node.js backend. The following code is when both the client and server are hosted locally. The code also functions properly when the swf is hosted locally connected to the server hosted on heroku. The socket only fails when both the swf and the server are hosted online.
Node.js Code (Server)
var WebSocketServer = require("ws").Server
var net = require('net');
var http = require("http")
var express = require("express")
var app = express()
var crossdomain = require('crossdomain')
var port = process.env.PORT || 5000;
app.use(express.static(__dirname + "/"))
var xml = crossdomain({ domain: '*' });
app.all('/crossdomain.xml', function (req, res, next) {
res.writeHead(200, { 'Content-Type': 'application/xml; charset=utf-8' });
res.write(xml);
//res.write(new Buffer([0x00]))
res.end();
});
var server1 = http.createServer(app)
server1.listen(port)
console.log("http server listening on " + port)
var wss = new WebSocketServer({server: server1})
wss.on("connection", function(ws) {
console.log("Connected");
ws.on("close", function() {
console.log("websocket connection close")
})
})
Crossdomain file - example.com/crossdomain.xml
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" secure="false"/>
</cross-domain-policy>
AS3 Code (Client) - I'm Using (https://github.com/theturtle32/AS3WebSocket)
import com.worlize.websocket.*;
Security.loadPolicyFile("http://example.com/crossdomain.xml");
var websocket:WebSocket = new WebSocket("ws://example.com", "*", "echo-protocol");
websocket.addEventListener(WebSocketEvent.OPEN, handleWebSocketOpen);
websocket.connect();
var con = false;
function handleWebSocketOpen(event:WebSocketEvent):void {
trace(event);
}
When the client is accessed from the server a request for the crossdomain.xml does occur and it gets returned successfully to the client, but at that point nothing happens anymore. I've been trying to solve this for ages with no luck. I did find that when you return the crossdomain file you need to end it with a 0x00 for it to be properly read, but I haven't found a way to make to that work. I've tried adding res.write(new Buffer([0x00])) to the response from the crossdomain request as well as several other modifications to no avail. These all seem to stop the xml file from being valid anyway.
I would greatly appreciate if anyone could help in getting this to work, I've been struggling with it for an extremely long time and can't quite seem to get this last step working.
The net module has a createServer function that allows you to create a network wrapper. This works fine on a local runtime of Nodejs, but when running in Bluemix it is unable to determine the host address. The server seems to get created, but upon further inspection I find the server.address to be blank.
var tls = require('tls');
var fs = require('fs');
var net = require('net');
var tunnelHost = (process.env.VCAP_APP_HOST || 'localhost');
var tunnelPort = 8888;
var server;
var gatewayOptions = {
host: 'http://cap-sg-prd-5.integration.ibmcloud.com/',
port: '15133',
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
ca: fs.readFileSync('ca.pem')
};
console.log("starting createSecureTunnel");
//create a server end point to use as a network wrapper for the secure gateway
server = net.createServer(function (connListener){
console.log('net server created');
connListener.on('end', function() {
console.log('client disconnected');
});
connListener.on('uncaughtException', function(err){
console.log('exception caught: ' + JSON.stringify(err));
});
//connect to farside, local/private server
connectFarside(connListener, function(err, remoteSocket){
if (err){
console.log(err);
}
console.log('connection made');
remoteSocket.pipe(connListener);
console.log('remote socket connecte to local connListener');
connListener.pipe(remoteSocket);
console.log('local connListener connected to remote socket');
});
});
//setup listener for network wrapper
server.listen(tunnelPort, tunnelHost, function(){
console.log('tunnel created at: ' + tunnelHost +":"+ tunnelPort); //.address +":"+ server.address().port);
});
//createa a TLS connection to the secure gateway
function connectFarside(conn, callback) {
console.log("starting connectFarside");
try {
console.log("initiating farside connection");
var socket = tls.connect(gatewayOptions, function(){
console.log("tunnel connected to " + gatewayOptions.host +":"+ gatewayOptions.port);
callback(null, socket);
});
socket.on("error", function(err){
console.log("Socket error: " + JSON.stringify(err));
});
} catch(err) {
console.log(err);
callback(err);
}
}
Bluemix gives your app a port to run on, this is the reason it is not working in Bluemix. You are starting to start your app on port 8888 with the following line of code.
var tunnelPort = 8888;
It should be changed to
var tunnelPort = process.env.VCAP_APP_PORT || 8888;
The above line will read an environment variable called VCAP_PORT where Bluemix assigns a port to your app, if it is not running Bluemix it will run on port 8888.
Your app will be accessible over the web on port 80 and 443. Bluemix will load balance to your app for you.
You can specify the server address when listening to the server
var net = require('net')
var server = net.createServer(handler)
server.listen(port, address)
Try with address = '0.0.0.0' and see if it works
Partially solved by using the cf-autoconfig module. It helps to reconfigure modules for use on Cloud Foundry platforms. By including this as the first line in my app, it mostly works. It doesn't use the port number. But at least I can access the wrapper.
So I added this as the first line
require("cf-autoconfig");
Then I changed the server.listen to this
//setup listener for network wrapper
server.listen(tunnelPort, function(){
console.log('tunnel created at: ' + tunnelHost +":"+ tunnelPort); //.address +":"+ server.address().port);
});
Now if I use my app name, I can connect to the server created by net.createServer().
I would still like to know how to get the port to work, so this can be used inside of a web application to provide the tunneling.
I have a node.js project that works fine on my local machine running a node server. However when I deploy it to azure I can not connect to the websocket server. I heard somewhere that you may need to edit the web.config file to turn on web sockets, but I cant find that anywhere.
The server sets up a websocket like follows:
var http = require('http'),
port = process.env.port || 1337,
NodeSimpleRouter = require('node-simple-router'),
router = new NodeSimpleRouter(),
WebSocketServer = require('ws').Server,
wss = new WebSocketServer({port: 8080});
//create the server
http.createServer(router).listen(port);
console.log('Web server running on port ' + port);
and the client like this:
var socket = new WebSocket('ws://localhost:8080');
do I need to change any of these settings, such as the value of 'localhost'?
Azure can be a pain sometimes with websockets and node.js . Here is what I got to work using socket.io . I have my code deployed out as a cloud service. Be careful if you do VIP swaps from staging to prod because I have noticed it doesn't play nice with websockets.
app.set('port', process.env.PORT || 3000);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'));
//Chat room
io.sockets.on('connection', function (socket) {
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
You can also override the websockets and force default of long-polling by using this:
//Over ride the Azure defaults
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});