Reputation: 23
I've made myself a chat application with node.js and socket.io and it works however I don't want to run it from localhost:80 anymore, I want it to be online. How can I port forward my web app? I've tried to portforward a HTTP port but that didn't work. Here's my sever side code:
var express = require("express"),
app = express(),
server = require("http").createServer(app),
io = require("socket.io").listen(server);
server.listen(80);
app.get("/", function(req, res){
res.sendfile(__dirname + "/index.html");
});
So yeah, all I want to do is instead of having to use "localhost:80" I want to be able to use my IP address to connect.
Upvotes: 0
Views: 2678
Reputation: 707318
Normal port forwarding from your home router should work just fine. A socket.io connection is a webSocket connection underneath and a webSocket connection starts life as an HTTP request and the same original socket is then "upgraded" to the webSocket protocol. So, client makes HTTP request to server to start the whole process and establish the original connection.
So, any port forwarding configuration that will work for HTTP should work for a socket.io connection.
Socket.io connections are long lived connections (unlike TCP connections) so you also have to make sure there is no infrastructure that won't allow a long lived connection (proxies often need to be specially configured for long lived webSocket connections), but this should apply to port forwarding.
FYI, you don't need .listen()
for the socket.io connection. You can change this:
io = require("socket.io").listen(server);
to this:
io = require("socket.io")(server);
In case this is not clear to your with port forwrarding...
When you are doing port forwarding on your home router, you direct incoming requests on a certain port to a specific computer and port on your home network. Then, when you want to connect from the internet, you connect to your router's public IP address and the port that you have configured for port forwarding. The router then uses the port forwarding rule to reroute any incoming request on the desired port to the specific server on your local network. You have to be very careful about this because if you make mistakes or don't have your app properly secured, this could open up a point of attack where people on the internet might be able to attack the internals of your home network.
Usually, this is not a long term scheme and it's better to pay a hosting company to run your app. Then, you can also get a permanent IP address and can set up a domain name to correspond to that IP address.
Upvotes: 1