Flutter Socket IO with nodejs - receiving timeout on connection - node.js

I'm using flutter socket io to communicate to my server that's running node/express.
The server code:
var express = require('express');
var bodyParser = require('body-parser')
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongoose = require('mongoose');
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}))
var Message = mongoose.model('Message',{
name : String,
message : String
})
app.get('/', (req, res) =>{
res.send("Hello");
});
io.on('connection', () =>{
console.log('a user is connected')
});
var server = http.listen(8080, "<MyServerIP>", () => {
console.log('server is running on port', server.address().port);
});
My Flutter code :
connect() async {
try {
String connectionPoint = "http://<MyServerIP>:8080";
//Connect to Socket.IO
socket = IO.io(
connectionPoint,
OptionBuilder()
.setTransports(['websocket']) // for Flutter or Dart VM
//.disableAutoConnect() // disable auto-connection
//.setExtraHeaders({'id': tokenId}) // optional
.build());
//socket.connect();
socket.onConnecting((data){
print("Connecting");
});
socket.onConnectError((data) {
print("Error Connecting - > $data");
});
socket.onConnectTimeout((data) => null);
socket.onDisconnect((data) => null);
} catch (e) {}
}
When ever i try to connect I'm getting a timeout error that's caught in onConnectError.
The node server is running debian, and I've checked the firewall status:
To Action From
-- ------ ----
27017 ALLOW Anywhere
80 ALLOW Anywhere
3000 ALLOW Anywhere
22 ALLOW Anywhere
Samba ALLOW Anywhere
8080 ALLOW Anywhere
27017 (v6) ALLOW Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
3000 (v6) ALLOW Anywhere (v6)
22 (v6) ALLOW Anywhere (v6)
Samba (v6) ALLOW Anywhere (v6)
8080 (v6) ALLOW Anywhere (v6)
3000 ALLOW OUT Anywhere
3000 (v6) ALLOW OUT Anywhere (v6)
When I open the url via chrome I'm getting the "Hello" message.
When I try netcat "nc -vz MyServerIp 8080 and I'm getting a success in connecting. I've also checked my local firewall and I've allowed all connections for qemu to my MyServerIp.
Just need some help as to try to work out why I'm getting the timeout and whether there are any ways to debug this ?
##Edit:
MyServerIP is the actual server ip of my server.
##Edit 2:
I used my device to test whether it was something that was local issue to the android emulator. And, I received the same error (I also took the device off my wifi to eliminate any local firewall issues). I'm assuming that this would mean that it has something to do with my server.

The issue being faced was due to the mis-match between the server version and the package being installed with Flutter. The version on the server was > 3. The latest version installed for flutter is not compatible with a server version. There is a beta version that needs to be used.
At the moment there are two options :
Option 1: Downgrade the servers version so that its compatible.
Option 2 use the beta version. (this is the option I took).
Update pubspec.yaml to socket_io_client: ^2.0.0-beta.2 and it connects.

Related

how to force node.js pm2 to run based on ipv4

currently my hosting uses a new firewall, and according to their plan, they don't allow any http connection based on ipv6 and all connections should use ipv4.
I have a service using node.js and expressJs, and I also use pm2 as a process manager to run my application, my problem is that http requests failed due to using ipv6. How could I force node.js to listen to version 4 IP address on nodeJs app.
The part of my code which I listen to a port:
const app = express();
...MANY MIDDLEWARE app.use();
mongoose.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true})
.then(result => {
app.listen(APP_PORT);
socketServer.listen(SOCKET_PORT, function () {
console.log('server listening to: %j', socketServer.address())
});
})
.catch(err => {
console.log
});
Can I use something like below with express:
var server = http.createServer(app).listen(APP_PORT, APP_IP);
I was curious about it too, but based on a useful link it worked like a charm :
https://github.com/nodejs/node/issues/18041.
I had this block of code :
var server = http.createServer(app);
server.listen(port);
In order to listen on ipv4, I made the following change :
var server = http.createServer(app);
server.listen(port,"127.0.0.1");
Giving the try one more time the port was listening with ipv4, otherwise it is listening on ipv6 :
$ sudo netstat -lntp |grep -i 3000
tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN 20501/node /home/ml

node.js Expess https gives ERR_CONNECTION_REFUSED

nodejs version v6.11.5
When hosting via Express, the website loads, i.e,
app.listen(port, function () {
console.log("The Server Has Started!");
});
But, when hosting via https, I get ERR_CONNECTION_REFUSED error. For hosting with https, I'm using
var options = {
ca: fs.readFileSync(__dirname+'/keys/sitname.ca-bundle'),
key: fs.readFileSync(__dirname + '/keys/sitname.key'),
cert: fs.readFileSync(__dirname + '/keys/sitname.crt'),
};
var app = express();
.... the app .....
https.createServer(options, app).listen(port, function(){
console.log("server started at port "+port);
});
The port is set to 80 and the ufw rules are as following
To Action From
-- ------ ----
22 LIMIT Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
22 (v6) LIMIT Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
Any response will be highly appreciable.
For https you have to set the port to 443 because https uses this port by default. Another option would that you define that you want to use port 80 for https when you send your request: my-domain.com:80. But I'm not sure if this works.
From #floriangosse's comment,
HTTPS uses port 443 by default and thus using port 80 for HTTPS gives ERR_CONNECTION_REFUSED.
Switching to port 443 worked.

This site can’t be reached on Nodejs Express?

I'm totally new to Node and I tried to run a test site on a hosting centos 7 (vultr.com). I've got nodejs, express installed.
Hello.js
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
run node hello.js
On my PC, http://x.x.x.x:3000/ => shows This site can’t be reached
x.x.x.x took too long to respond.
UPDATE:
I think you should consider about your server port. Have you open port 3000 in CentOS?
You can check your open port by typing
iptables -L
I think the firewall blocked your port you can open it by type this command
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
If you are using remote server, probably your 8080 port is blocked.
If you have root access and port 80 is open you can try and run script with sudo
But the first option is probably your problem
If you use the Google Cloud platform, you can open port 3000 at FIREWALL RULES in VPC network.
It works for me.

Access node app on digital ocean - This site can't be reached

I am unable to access my digital ocean node js app. I've already SSH'ed in, cloned my Node app from Git, npm installed, and successfully started the app on the droplet, yet I get error
This site can't be reached
Digital Ocean docs say you can access your publicly facing website simply by going to <your website's ip>:<port>:
I did this by going to 67.205.185.63:9000/ (my app is running on port 9000 as you can see):
root#nodejs-512mb-nyc1-01:~/demos# npm start
live-demos#1.0.0 start /root/demos
node app.js
Demos is listening on port 9000
How else should I be accessing my node app?
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var port = process.env.PORT || 9000;
...
app.listen(port, function () {
console.log('Demos is listening on port ' + port);
});
Some Digital Ocean droplets (mainly one-click apps) come with ufw firewall installed and by default all ports except for 22, 80, and 443 are blocked.
To check if ufw is installed and which ports are blocked/open do:
sudo ufw status
Output:
To Action From
-- ------ ----
22 LIMIT Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
22 (v6) LIMIT Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
To allow traffic on port 9000 do:
sudo ufw allow 9000/tcp
Add the 9000 port by writing following command
sudo ufw allow 9000

Webpage not publicly accessible via port 80

I have the following code written with Node/Koa, which is serving to port 80:
const
koa = require('koa'),
route = require('koa-route'),
network = koa(),
common = require('koa-common'),
PORT = 80;
// enable logger middleware
network.use(common.logger('dev'));
// enable static middleware
network.use(common.static(__dirname + '/public'));
network.use(route.get('/', index));
network.use(route.get('/about', about));
function *index() {
this.body = "<h1>Is this message on my computer, or on yours...?</h1>";
}
function *about() {
this.body = "<h2>How about now...</h2>";
}
var server = network.listen(PORT, function () {
console.log('Listening on port %d', server.address().port);
});
I have reserved an address (168.192.1.91) for the host computer, set up port forwarding to this address on port 80, made an exception in the Windows 10 firewall for port 80 when connected to via any protocol, and tested with You Get Signal:
which confirms that the port is currently open. When I browse to localhost:80 I can see the default page. However, when I type the computer's public IP address into the browser (I'm typing in the one that I've partially obscured, which I believe should be the correct one):
this page fails to load with the following error:
This site can’t be reached
109.[...] took too long to respond.
Try:
Reloading the page
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_TIMED_OUT
and there is no activity in the Koa log (which logs fine when I browse to there via localhost:80). Any ideas what could be blocking the connection?
I have also tried adding the host address as a second parameter like this:
const HOST = "127.0.0.1";
var server = network.listen(PORT, HOST, function () {
console.log('Listening on port %d', server.address().port);
});
This works for the loopback address, but when I specify my public IP I get this error:
C:\Sites\order-server>node --harmony cheese.js
events.js:154
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL [My public IP here]:80
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at _combinedTickCallback (node.js:386:13)
at process._tickCallback (node.js:407:11)
at Function.Module.runMain (module.js:449:11)
at startup (node.js:142:18)
at node.js:939:3
Maybe koa by default only binds to 127.0.0.1 so you can try binding to any host explicitly with .listen(80, '0.0.0.0')
Otherwise if the firewall ant the port forwarding is configured accordingly, the problem could be that your Internet Provider is blocking incoming connections somewhere. You could try useing an Port over 1024, if it only blocks Ports below.

Resources