what port does the socketIO client listen to by default? - node.js

When you do not specify a port in the io.connect() function on the client side, what port does the client listen to?
eg:
var socket = io.connect('http://example.com/'); // listening to port 80?
The reason I ask is because I deployed the server side of this application on heroku. The front end is an IOS application. The objective C socketIO library I'm using for the front end (https://github.com/pkyeck/socket.IO-objc) requires me to specify a port. But I'm not sure what port I should listen to since this is not static on the server side.
I wrote client program in javascript for testing and it works when I do not specify a port.
Any help would be much appreciated.

It listens on the port the server listens to. Imagine you set your server to listen port 8080. Then you load your page at http://localhost:8080 and the server returns the page which contains the socket related JS code.
If you don't specify any port or host as in var socket = io.connect();, it defaults to the host and port of the current page.
If you specify only the host which is same as the current host, it defaults to the port of the current host.
If you specify only the host which is different from the current host, it defaults to port 80 if the protocol is HTTP. If the protocol is HTTPS, then the port defaults to 443.
Here is the related code for url.js parser.

Related

Socket.io on port 443

I have a little problem when I'm trying to load socket server on port 443 with node.js
The port is taken by IIS.
All my customers can use only port 443 to connect to my socket.
In my IIS I have 4 IP addresses.
I try to config the socket.io to use one of the IP's that are not configure in the IIs. But it didn't work.
Do I have any solution to use port 443 in socket server and also use it in IIS?
Thank you

How do you host a Node server from your computer so everyone can connect to it

I've made a node program and I want to host it trough my computer, how can I go about doing so. Currently I'm using "require("net");" function to start the server locally!
var net = require("net");
var server = net.createServer();
server.on("connection", function(socket){
//Stuff happens in here
});
server.listen(process.env.PORT || 6969, function(){
console.log("Server is listening to %j",server.address());
});
To run a Node.js application you have to install Node.js - you can download it HERE. Then in the command line / terminal, you navigate to the location of your code, and run Node.js with your file as an argument:
cd /c/some/location/with/your/file
node file.js
There is now a Node.js process that is listening on your chosen port - so it's hosted locally.
Looking at the Node.js documentation for Server.listen, it looks like the app is already listening for outside connections. The signature (or one of the signatures) is server.listen(PORT, HOST);. And I also see:
If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
So your app should already be contactable from the outside (although you may have to open the port explicitly depending on firewall rules). So some other computer, if they make a TCP/or whatever request to <the IP address of your computer>:6969 then the server will respond.
I'm actually surprised that the default host is 0.0.0.0 and not the loopback address (localhost or 127.0.0.1 - these are the same address).
Note not my answer look it up here:
https://serverfault.com/questions/271824/node-js-is-not-accessible-from-external-ips-on-ubuntu
You cant access node.js from outsiede because it is listening on localhost (127.0.0.1). You need to configure it to listen on 0.0.0.0, with this it will be able to accept connections on all the IPs of your machine.
server.listen({
host: '0.0.0.0',
port: 6969
});
If i understood your question correctly, you want to host your node app with your computer as a web server. It's pretty easy you can follow this blog.
Basically the steps include:
Getting a Domain name.
Registering your IP with DNS so that your
domain name points to your computer when someone enters that in the
browser.
Running the web server (Node) to handle the requests.
Note: You might face some problems if your ISP(Internet Service
Provider) changes your IP address frequently.
Since you clarified your question is about how can you host your application so other people can use it, it's not really Node-specific.
You have to open the appropriate ports in your system-level firewall and in your router as well (search for your router's model and you'll see it). Web applications always run in port 80 (for HTTP) and port 443 (for HTTPS), both in TCP. Do note that some providers won't allow you to host a server in port 80, so you'll have to try it out to check if that's the case for you.
Your Node application seems to be running in port 6969, so you either set it to use port 80 (which requires admin privileges) or use a reverse proxy to send all traffic to/from port 80 to port 6969. You can do that with nginx, for example.

express-ws: How to avoid typing the port on the request url?

I've successfully created a WS Server using Node.js and wxpress-ws. The problem is that I don't know which port to use to not type it in the request URL. So, instead of using ws://mysite.com::xxxx/, I need to be able to type only ws://mysite.com/
I've tried to listen port 80, 8080, without success.
If your application is listening on port XXXX then you will need to access it via indicating the port in your url ws://myapp.com:XXXX/.
If you want to use ws://myapp.com/, you will need to listen to the port 80, two solutions for you :
Launch your application with sudo node myapp.js privileges and make it listen on port 80. I don't recommend this approach since it might introduce some vulnerabilities because the app is running with admin privileges.
Set up some reverse proxy like nginx that will listen on port 80 and that will redirect the traffic to your application listening on port XXXX. You can find a lot of tutorials online

Why need to explicitly specify port 80 to access my website using HTTPS?

I followed the instruction to make my nodejs server listening to port 80.
https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps
My server does not have a domain yet. The problem is that if my server used HTTPS, I cannot access my server without specifying port 80 like this https://xx.xxx.xxx.xx:80. However, using HTTP, without specifying port, I still can access my website using IP.
What did I do wrong here?
Because the default port for https connections is 443. So browsers will connect on that default port if your url has a https protocol and no port number specified.
Since you are using port 80 (which is the default port for plain http), you need to specify the port number in your URL.
In short, you need to specify the port number in your URL if your server is listening on a port other that the default port for that protocol.
you can find the default port of a certain service in file /etc/services, If the server do not use these default ports, then you have to specify the port as you said to browse.

Setting the port for node.js server on Heroku

I launched a node.js server with the following line to set the port:
app.set('port', process.env.PORT || 8080);
This means that, it should either read the PORT env variable or default to 8080, as it does when it's run locally. Neither of them is happening on Heroku, and the server always uses the default port 80. Any idea how to change it?
heroku config
PORT: 8080
You can't. Heroku sets the PORT variable that you are supposed to bind, and listens on tcp/80.
You should use the port opened by heroku like so:
port = process.env.PORT || 80
It sets the port to 80 if it has somehow not been set already
All the answers are great! Wanted to touch on the theory a bit so people can understand why Heroku sets it port to 80 or 443.
Computers had to create a convention to communicate with each other with a different protocol. For example HTTP, HTTPS, SSH, FTP , etc.
As a result, there was an agreement that computers will communicate HTTP with port 80, https will communicate on port 443, and so on with the other protocol. Below is a table displaying all the protocol's conventional reserve port number.
Now some of you people may be thinking well if these are reserve port number how come my computer lets me use port number 80 and 443 (ex localhost:80). You can use any port number (in fact you can choose up to 65,535 port number) but once you want to deploy to live and want others to use your application then you will have to start using the convention of port 80 (HTTP) or port 443 (https).
Heroku makes it nice and easy for you by providing an environment variable process.env.PORT to apply for the correct conventional port number so others can access your app.
Heroku treats web apps just like any other app and doesn't allow you to assign listening ports directly. Your web server will be assigned a dynamic port by Heroku but to ACCESS it, you will need to use the default port (80).
On Heroku, apps are completely self-contained and do not rely on
runtime injection of a webserver into the execution environment to
create a web-facing service. Each web process simply binds to a port,
and listens for requests coming in on that port. The port to bind to
is assigned by Heroku as the PORT environment variable.
The contract with Heroku is for the process to bind to a port to serve
requests. Heroku’s routers are then responsible for directing HTTP
requests to the process on the right port.
Reference: Heroku Runtime Principles - Web Servers
You can do for example:
const PORT = process.env.PORT || 5001;
app.listen(PORT, () => console.log(`Server is listening on port ${PORT}...`));
In my case, heroku was listening on the default HTTPS port: 443 and it was not visible via heroku config:get PORT.
80 is the default port so use 80 instead of 3000 or 8000
const PORT = process.env.PORT || 80;
var server = app.listen(PORT, function() {
var host = server.address().address;
var port = server.address().port;
console.log("server is listening at http://%s:%s", host, port);
});
Different ports on Heroku can be used, as long as the main $PORT is also used.
See https://stackoverflow.com/a/43911373/9646899

Resources