Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying a TCP server to Heroku

I have a TCP server coded in node.js. I'd like to put it up on Heroku because it's a free service and I don't need anything more than what their free plan offers.

Now, I know very little about the inner workings of Heroku and I'm pretty new to the whole thing so I have a few questions.

Firstly, is it even possible to deploy a TCP (non-web) server? I've read that Heroku doesn't like node.js's net because it doesn't support websockets and that I should use socket.io.

So I've switched my server to socket.io. I think. Because my code more or less looks the same. I've done this as well: https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku

What do I put in my Procfile instead of "web"? Also, when I tried to deploy what I currently have, the logs said that my application failed to bind to $PORT. What's $PORT? And how do I change it to the port I want?

In fact, if I don't change it, how do I know what it is so my application can connect to this server?

like image 617
Luka Horvat Avatar asked Sep 03 '13 20:09

Luka Horvat


People also ask

Can you host backend on Heroku?

And, as you know, Heroku is a service that lets you get a back-end app up-and-running quickly. In this article, you'll learn how to deploy a simple, pre-built, Node.

Does Heroku need package JSON?

js that will be used to run your application on Heroku, should also be defined in your package. json file. You should always specify a Node. js version that matches the runtime you're developing and testing with.


2 Answers

Heroku doesn't support a generic TCP server but you should be able to get the functionality you want with socket.io.

You need to put web in your Procfile. That's what lets Heroku bind an external connection to port 80 to the local port your web traffic will arrive you. You find that port by looking at the environment variable $PORT. More info, with examples, is here: https://devcenter.heroku.com/articles/nodejs

like image 61
Dan Kohn Avatar answered Nov 03 '22 02:11

Dan Kohn


The free tier of Heroku does not support TCP server. Here is the reason.

To save costs and offer free services, Heroku hosts multiple free-tier apps on the same machine. These apps, one of them being yours, share the same IP address. The apps are assigned to different ports.

However, as you probably noticed, when you access your app in a browser, the port is always 80. Hence, to know which app an incoming HTTP request is looking for, the server must be looking into the HTTP headers. (For example, using HOST to find out the app name, then resolve the app name to an internal port number.)

Finally, Heroku decided to hide away the internal ports from the internet. This, along with the fact that TCP connections don't have a HOST field, makes it impossible to host a TCP server with Heroku.

To work around this, use WebSocket.

Appendix: the Research

  • Testing was done with a free-tier Heroku app, in March 2020.
  • If you make up a non-existing app name (e.g. https://hr.herokuapp.com), Heroku responds with a page saying "There's nothing here, yet."
  • If you first manually nslookup an existing app (e.g. https://world-of-blogs.herokuapp.com), then try to use the IP address to access the app, Heroku also responds with a page saying "There's nothing here, yet."
like image 44
Daniel Chin Avatar answered Nov 03 '22 00:11

Daniel Chin