Answer the question
In order to leave comments, you need to log in
How can a web page find out the port of a WebSocket server raised in Heroku?
To try out Node.js, I'm writing a small chat on web sockets: https://github.com/zeromodule/zerochat
I still don't understand anything in the node.
When the port that WebSocketServer listens to is specified explicitly and registered both in the client (main.js) and the server (server.js) - everything works well locally, on its virtual machines, etc.
But now it became necessary to test this project on Heroku, and there the port to which the server can be bound is chosen dynamically by Heroku and should be specified in the server code like this: process.env.PORT || 5000
Server code from their own test application: https://github.com/heroku- examples/node-ws-test/bl... The
question actually is, how does the client know what port is there now, because he needs to know this in order to connect?
What is the best way to do this?
By the way, I don’t understand how their test application works (and it works), because the port is not specified in the client at all : https://github.com/heroku-examples/node-ws-test/bl...
Answer the question
In order to leave comments, you need to log in
It's simple, let nginx proxy all websockets requests from port 80 to your application's port. You can take the value from ENV. You can also make a request to the server to pick up the port.
In the example, the port is not specified because the application also runs on the same port. It simply takes the current host (domain name + port).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question