Allow Public IP to connect via WebSockets
networking, node.js, portforwarding, router, websocket
Solution
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.
Problem
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.