Data sent over TCP not getting delivered - node.js

I'm working with node.js looking to build a chat application with p2p capabilities.
I have 2 seporate computers on 2 different wifi networks.
I'm using the module fully-connected-topology and adding users to the topology using t.add(IP:PORT)
I'm getting the IP using the public-ip module and publicIp.v4().then(ip => { (Gets external IP)
I've tried getting the port in 2 ways:
Using get-port module (Finding open ports) and getPort().then(port => {
register-multicast-dns module and hash-to-port (I think this only works for local host so I switched to looking for open ports
Here's where I'm stuck:
Computer 1: IP = IP1, PORT = PORT1
Computer 2: IP = IP2, PORT = PORT2
when I run on Computer 1 t.add(IP2:PORT2) and on Computer 2 t.add(IP1:PORT1) no connection is made.
I know this because in this code:
t.on('connection', function (socket){
console.log('Connection established')
})
the print statement never fires.
Can someone direct me to where the packet is getting lost/dropped?
It all worked when on the same network but I can't find where the error lies.

Related

send https request nodejs express from another computer

how can i post / get data from nodejs server using another computer ?
so i'm connecting too computers using ethernet cable ,and i was able to open the website (react ) but
when i try to post or get data i get this error ( from the one who's not running the server on )
POST http://localhost:8080 net::ERR_CONNECTION_REFUSED
the too computer are running on the same network
i replaced localhost with the local IP address but still same error
i also added my local ip adresse to the app.listen
app.listen(PORT,'169.254.xxxx', () => {
console.log(`Server is running on port ${PORT}.`); //PORT = 8080
});
but still getting same problem

Connection to external postgres database times out

I am trying to connect a client device to a PostGreSQL host device. I have 2 devices with PostGreSQL on them: A Windows OS device (Host) and a RaspberryPi running raspbian (Client).
I have pgAdmin on the host device, with a PostGreSQL server running on it (The server is the default PostGreSQL server with default configurations).
The host device and the client device are on the same network (wifi).
I have the following node.js code on the client device:
const { Client } = require('pg');
const connectionString = 'postgres://postgres:MyPassword#192.168.1.8:5433/postgres';
const client = new Client({
connectionString: connectionString
});
client.connect();
client.query('CREATE TABLE test (something text, number int)', function(err,result){
if (err) throw err;
console.log(result);
console.log('hi');
});
based on the code above, the client would connect and query the making of a table, then return the result and 'hi' in the command line.
When I run it, it does nothing. After a minute or so, the connection times out.
Is there anything that I'm missing?
I used Wireshark to see if the client device was even sending packets. The client device was sending packets (directed to the postgresql post), so my assumption is that the problem is with the host device, but I don't know why because I have already edited the configuration files multiple times by now.
I have already...
changed config files
restarted the system
re-installed the software
switched to trying to connect from psql
Well, this is interesting...
So I found the real .config files on the host device, and I changed listen_addresses = '*' to listen_addresses = '192.168.1.12'.
After I made those changes, I tried to restart postgres and then the service stopped, but refused to be started again. I opened pgAdmin 4, and none of the servers were wanting to connect. After I changed the file back to listen_addresses = '*' the service started and everything was back to normal. My conclusion now is that there is a problem with the firewall. Does anyone here do Windows and can help me configure the firewall??
Turns out, I was bothering myself with the Postgres configuration files, but I had to look at the firewall configurations. Those were the ones that were blocking the pi. If anyone has the same problem, I suggest looking at your firewall first. (Specifically allowing port 5432)

Making my Node app accessible to the Internet from a local machine

OK so I know I can use cloud hosting and I've done so before but I am doing a demo and I want my node app to be on my local machine but accessible from the internet. Here is how I start the server in the server file
const port = 8080;
var server = http.createServer(app).listen(port, () => {
console.log(`Server listening on port ${port}`);
});
Next I set up port forwarding on my Xfinity gateway such that both port 80 and port 8080 point to my desktop. I know I am connecting to the right device because SSH works from outside of my network on port 22. However when I enter [public IP]:8080 I am unable to receive a response. The only time I receive a response is when I enter 10.0.0.58:8080 which refers to my internal network. Why is this???
I personally like ngrok to do the same thing. It's really easy to setup and I found it really stable.
Give it a go https://ngrok.com
After installing you can simply forward ports like
ngrok http 8080

Running peerjs-server on OpenShift returns 503 Service Unavailable

I have set up up a node.js 0.10 gear in OpenShift which I deployed a simple server which is based off peerjs-server. All I want this server to do is act as a signalling server to communicate the connection info between peers connected to my application and from then on they communicate peer-to-peer using WebRTC. Everything works when pointing to the demo "PeerJS Cloud" signalling server but when trying to use my own server set up I keep getting returned 503 status codes.
Here is the server creation code I use:
var host = process.env.OPENSHIFT_NODEJS_IP;
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server = new PeerServer({ port: port, host: host});
NB: I have added host to peerjs-server so I can use OpenShift's IP, not sure if this was necessary but it wasn't working without this either.
The server peerjs-server uses is restify. Here is the server create and listen code:
this._app = restify.createServer(this._options.ssl);
/* A lot of set up code that I have not changed from peerjs-server */
this._app.listen(this._options.port, this._options.host);
Where this._options.port and this._options.host are the ones defined in the previous code segment and I am not using SSL so nothing is being passed in there.
When deploying this code to OpenShift I get no errors but when accessing the site on port 80 or 8000 (the open external ports) I get 503's. I also checked rhc tail and this is what I get:
Screenshot (Can't post images because I have no reputation..). Not sure exactly what that means if anything.
Any help is much appreciated, and if more info is needed I can add more, was not sure what was important information or not.
UPDATE: It's a scaled application using 1-3 small gears.
from https://github.com/peers/peerjs-server/blob/master/lib/server.js:
// Listen on user-specified port and IP address.
if (this._options.ip) {
this._app.listen(this._options.port, this._options.ip);
} else {
this._app.listen(this._options.port);
}
So, use 'ip' and not 'host'. Worked for me.

Node.js: Cannot access server from different device on the same network

NOTE: There are some others who have had similar problems, but those were solved by fixing small tidbits in the code involving how the server was listening (in examples that I've seen they put '127.0.0.1' as an argument in http.createServer(...).listen(). However, I do not have the same issue.
When I try to connect to my node.js server from a different machine on the same LAN network, Chrome says that it cannot connect.
This is testtesttest.js
var http = require('http');
http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end('Working');
}).listen(3000);
When I try inputting 192.168.1.73:3000 (of course 192.168.1.73 is the ip of the machine that I'm running the server on) into the browser (Chrome, although I've tried other browsers as well and I have similar problems) of my other machine, it gives the error "Oops! Google Chrome could not connect to 192.168.1.73:3000". When I type the same address onto the local machine, it works fine.
I'm not exactly sure what to do. I honestly hope this is just a stupid mistake on my part (I'm sorry for possibly wasting your time) and not something that I have to go into my router for.
Thanks very much for any help.
Try changing this
.listen(3000);
to this
.listen(3000, "0.0.0.0");
Just putting this here in case it saves anyone else. I had this problem for two full days when trying to connect my phone to my local machine... and it was because the wifi on my phone was turned off.
I too had this problem.
I solved it by allowing "node.js" in the network group.
Solution : Allowing "node.js" through the private network windows firewall
a. Go to Control Panel
b. Go to Windows Firewall
c. Click on the Allow an app or feature through windows firewall in the left sidebar
d. Search for "Node.js : Server Side JavaScript" and make sure both public and private column box is marked tick for "NodeJS"
e. Click "OK" and you are done
Below is the step i followed which Worked
My server code
var http=require('http');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/plain'});
response.end('Im Node.js.!\n');
console.log('Handled request');
}).listen(8080, "0.0.0.0");;
console.log('Server running a http://localhost:8080/');
Added inbound Rules.
Created a udp inbound rule(since i could'nt find any http protocol).
Once created go to properties for the created rule.
Choose Protocols and Properties tab.
Choose any in Port Type. Click apply and Ok. Now i tried from other
machine it worked!!!
I think you need to set Port Type to any to make it work.
Thanks
You have to open 3000 port so that it can be accessed from remote machines. You can do that in iptables file. Use below command to open file
vi /etc/sysconfig/iptables
now add below line before the reject lines in that file
-A INPUT -p tcp -m tcp --dport 3000 -j ACCEPT
Now restart iptables service using below command
service iptables restart
Now you restart your server and try again. It should work..
Chances are your firewall settings block incoming request on port 3000. You may want to add firewall inbound rule on this port to allow access to it.
For me, the culprit was a VirtualBox Host-only Network interface. The presence of this network interface was causing ipconfig on the server to report 192.168.56.1 instead of the router assigned address of 192.168.1.x. I was accessing the wrong IP all along.
To remove the VirtualBox Host-only Network interface:
Open VirtualBox
Go to File>Preference>Network>Host-only Networks
Remove the offending adapter(s)
My problem was that, I have used IP assigned to my ethernet adapter instead of wifi adapter...
And it now works when I connect from any device.
http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end('Working');
}).listen(3000, "192.168.1.36");
I found my IPv4 address on network settings, then specify with listen fun. put also 3000 port. I can reach http://192.168.1.36:3000/ via my tablet which is connected same wifi.
You have to run that on the terminal:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Explanation here
Like what Umamaheswaran answered, a new inbound rule needs to be created. But instead of using the UDP protocol, I have to use TCP. My application runs on a Windows Server 2012. The Inbound Rules are set in Windows Firewall with Advanced Security, under Administrative Tools
I solved this using this algorithm. What it does is get all ip info from os.networkInterfaces, get all keys and loop trough it, filter by objects that contains family == "IPv4" and get it address value. After, it looks for '192.168.0.xxx' pattern and put it in possible_ip array. Any other ip will be pushed to other_ips array.
The export sentence is to turn it into a module js file.
import by import {get_ip} from ./your_path/name_of_your_file.js, or in common js const { get_ip } = require("./your_path/name_of_your_file.js");
Hop it helps
const os = require('os');
const _ = os.networkInterfaces();
// if common js use
// exports.get_ip = () => {
export const get_ip = () => {
let ips = [];
Object.keys(_).map(key => {
let ipv4_obj = _[key].filter(obj => obj.family === "IPv4")
let ipv4 = ipv4_obj[0].address
ips.push(ipv4)
})
let other_ips = []
let possible_ip = []
ips.map(ip => {
ip.includes("192.168.0.") ?
possible_ip.push(ip) :
other_ips.push(ip)
})
return { possible_ip, other_ips }
}

Resources