Trouble running socket.io in ports other than 80 - javascript

I'm running express.js on port 80, so socket.io has to go on other port. The problem is that, then, I have to load socket.io on the client with:
<script src="http://my_domain:8080/socket.io/socket.io.js"></script>
Which is horrible because I have to change my_domain everytime I switch from local machine to host.

I'm running express.js on port 80, so socket.io has to go on other port.
Wrong.
You can run Socket.io and Express on the same port; just pass same same http.Server instance to both of them.

Related

What is the significance, if any of port 9229, and can it be changed?

By default, I was taught, when I run a local Node server to run it on port 3000.
After reading a tutorial on the Chrome Node debugger which is set to inspect port 9229, I switched to this port.
Here is a similar Q / A on port 3000.
Here is the tutorial.
Can I switch the default to 3000 some how?
This way it will match my Express local port:
app.set('port', (process.env.PORT || 3000));
Or would it be more prudent to change my local express server to 9229?
I know this is a super old question in SO time but if anyone else comes here for whatever reason, here's the skinny.
TLDR; Port 3000 is for your application, Port 9229 is for the NodeJS debugger.
Port 9229 is the default NodeJS debugging port. It allows you to attach a debugging tool like Chrome's Inspector or WebStorm's Remote Debug to a Node process that is running with a special flag (node --inspect server.js in Node v8+ I think, no idea what it was before that). This has nothing to do with what port your listing to in your HTTP application.
Port 3000 is the "recommended" port to run HTTP applications like Express, Koa, Hapi, etc. I have no idea when it was decided that port 3000 was the goto but here we are.
If you want to read more about NodeJS debugging here's a link.

GCP deployment port issue for a node js project

I have deployed one node js project in which a http server is created at port 8080 and listening at 8080 and a service url is generated.But inside http.createServer i am creating another server for a nlp engine which is listening at port 8081.So using the service url generated after deployment i am able to hit server running at 8080,but how to access the nlp engine server running at 8081 using the same service url with out using router?ordo gcp doesnt allow creation of two server listening at two ports with the same service url?
Can you try http://hostname:8081? what does it say?
If the port 8081 is open, then ideally you should be able to access that NPL engine, else you may need to get the port open in GCP.

Communicating over socket.io with a heroku app

I have two applications that need to communicate with each other. One is a node app deployed on heroku. The other is a node app deployed on a raspberry pi3. I need the raspberry pi3 to send the heroku app data using socket.io
On the heroku app I have
http.listen(process.env.PORT, function() {
console.log("listening on *:" + process.env.PORT);
});
Which listens on the port Heroku assigns.
On the raspberry pi app I have :
let socket = io(`http://appName.herokuapp.com:7256`)
I logged out the port number from heroku when it connected and it was 7256. However, I am unable to communicate with the application on Heroku. There are not any errors when I am trying to emit from the pi. But the Heroku app isn't receiving anything. Is there anything else I need to do?
Use port 80 (or no port at all, to default to 80).
The PORT env var is an internal port - it's the port within the Heroku container that your app should listen on.
Externally, Heroku's routers expose your app online at the normal ports: 80 and 443 (http and https).

I don't have to specify a port for heroku for my socket.io app?

I've successfully made a test chat app and I've gotten a node js server with socket.io running on heroku. On my local computer I have to specify the port number of localhost on the client side to the port that the server has set up. However, when I run my server code on heroku. Removing the server
I'm using the process.env.PORT variable since heroku sets that up:
var port = process.env.PORT || 3000;
http.listen(port, function(){
console.log('listening on *:' + port);
});
Naturally I find the port number that the app is running on place it in the url
var socket = io('https://xxxx.herokuapp.com:1111');
However this gives me an "net::ERR_CONNECTION_REFUSED".
I got it to work by removing the port nubmer after the url (in this example :1111). I'm wondering why this is working since most of the tutorials and articles online have it specifying the port and why my local computer needs the port to work as well.
When you connect to your https://xxxx.herokuapp.com subdomain on heroku on port 443 (which is the port that is used for an https connection when no port is specified), Heroku is probably using a proxy or router to route that incoming connection to the particular port that your node.js server is listening to. In the Heroku infrastructure, they know what actual internal host your server is running on and what actual port number it is running on so they can map a default port request on your subdomain to the actual port/host.
This is done so that browsers can connect to your subdomain directly on the default port without having to know the particulars of your node server installation and so that Heroku can auto-manage your server and likely share hardware with other customers. You are each running on a different port, but sharing the same machine. The ports are managed entirely by Heroku and this is one way that they are able to put multiple customers on the same hardware without each having to specify a custom port in the browser URL (which would be a non-starter for most customers).
So, Heroku is hosting some sort of proxy for your sub-domain that is listening to the default https port. Thus, you don't have to specify the port in the URL. Internally, they route that connection to your actual port on your actual server.
When running on your desktop, there is no such proxy to do this for you so you have to make sure client and server port numbers match.

OpenShift NodeJS deployment : socket.io index.html port assignment, etc

I locally wrote a nodeJS app using socket.io and express modules.
I wanted to use openshift for hosting.
So I changed the main .js to server.js which seems to be the index equivalent of the openshift file and changed the server port setting to:
var server = require('http').createServer(app).listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);
as indicated in some posts.
However after git commit, I am still getting:
remote: info: socket.io started
remote: warn: error raised: Error: listen EACCES
remote: DEBUG: Program node server.js exited with code 0
remote:
remote: DEBUG: Starting child process with 'node server.js'
and the website doesn't work.
As the app is serving a html file, there are two more places, where the port is mentioned, which sit in the index.html that is served:
header:
<script src='//localhost:3000/socket.io/socket.io.js'></script>
and within javascript for the html file:
var socket = io.connect('//localhost:'+process.env.OPENSHIFT_NODEJS_PORT || 3000);
// intial vars and multi list from server
socket.on('clientConfig', onClientConfig);
All files and modules are seemingly uploaded, but the EACCES error still prevails.
I get the feeling that maybe the header link to localhost:3000 might be the skipping point, but I am not sure. Anyone have any idea, what the problem is?
Also, there is no : socket.io/socket.io.js file in the socket.io modules folder, which I find confusing.
I had recently developed a chat client application using socket.io and also had webrtc in it. I was able to deploy the app on openshift by making the following changes into code.
Client Side
Keep the include script tag in a relative manner like so
<script src="/socket.io/socket.io.js"></script>
While declaring io.connection, change the ip part to point the application to server in this format.
var socket = io.connect('http://yourapp-domain.rhcloud.com:8000/', {'forceNew':true });
8000 is for http and 8443 is for https
Server Side
The io and the server should both be listening on the same port and the order in which the statements are run should also be given attention.
Step 1: Declare the http server using app.
( app is obtained from express)
var express = require('express');var app = express();)
var server = require('http').Server(app);
Step 2:
Declare io from socket.io and combine it with the server object.
var io = require('socket.io').listen(server);
Step 3:
Now, allow the server to listen to openshift port and ip.
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
Please pay special attention to the order of the statements you write, it is the order which causes issues.
The server side of your websocket needs to listen on port 8080 on your openshift ip address, the CLIENT side needs to connect to your ws://app-domain.rhcloud.com:8000
I have a few notes on how to use WebSockets here: https://www.openshift.com/blogs/10-reasons-openshift-is-the-best-place-to-host-your-nodejs-app#websockets
You don't need any additional server-side changes after adapting your code to take advantage of environment variables (when available)
OpenShift's routing layer exposes your application on several externally-accessible ports: 80, 443, 8000, 8443.
Ports 8000 and 8443 are both capable of handling websocket connection upgrades. We're hoping to add support for WebSocket connections over ports 80 and 443 soon.

Categories