Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow Public IP to connect via WebSockets

I tried to creating a chat with nodejs ws (einaros),this is my code:

Server:

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 80 });
wss.on('connection', function(ws) {
    console.log('connecting count:' + wss.clients.length);
});

Client:

var hostname = location.hostname;
var port = 80;
var url = 'ws://'+hostname+':'+port+'/';
window.WebSocket = window.WebSocket || window.MozWebSocket;
var w = new WebSocket(url);

when I test with my computer or some other computers who connect with my router, it works well. However, other clients who visit via Internet cannot make the connection. Of course, they use the public IP, like 218.xxx.xxx.xxx, not 192.168.xxx.xxx.

I wonder how to solve this problem.

Thank you for you answer. But, it may be not the NAT problem. For example, My public IP is 218.100.50.50,and My private Ip is 192.168.1.1. When a connection comes from outside my network and visits 218.100.50.50:80, the router will redirect it to 192.168.1.1:80. There's a web page which can be visited in this way, but still cannot make the connection to my websocket server. This problem really confuses me much.

like image 653
Nagi Avatar asked Feb 24 '14 04:02

Nagi


People also ask

Is there any way to make WebSocket server from local network?

is there any way to make websocket server which I can connect same time from local network also from public network ? You need to add a "port forwarding" rule in your exposed router, otherwise port 81 is not accessible to outside world. Yes I forwarded ports 80 and 81 to my local IP.

How do I publish a website on a public IP address?

Use the public IP feature in Azure VMware Solution to publish the website on a public IP address. You'll also configure NAT rules on the firewall and access Azure VMware Solution resource (VMs with a web server) with public IP. To enable egress traffic, you must set Security configuration > Internet traffic to Azure Firewall.

How do I add a public IP address to a hub?

Select Secured virtual hubs and, from the list, select a virtual hub. On the virtual hub page, select Public IP configuration, and to add more public IP address, then select Add. Provide the number of IPs required and select Add. Once all components are deployed, you can see them in the added Resource group.

How do I enable public Internet access in Azure application gateway?

You enable public internet access in two ways. Host and publish applications under the Application Gateway load balancer for HTTP/HTTPS traffic. Publish through public IP features in Azure Virtual WAN.


1 Answers

Sitting behind router with IPv4 leads to problems for routing actual traffic to right direction (computer behind router).

You need to Forward port 80 in router settings to your computer IP. Additionally I would recommend set in router settings preferred IP for your computer so after restart it is more likely not to change local IP address (otherwise can happen).

After those changes, you can even try to connect using Public IP from your own computer, and if it will work - then it should work from external computers.
Additionally check firewall settings to make sure it does not blocks any external traffic to port 80.

like image 157
moka Avatar answered Oct 13 '22 01:10

moka