Node.js on Heroku with 2 ports - node.js

I'm trying to push a node.js app to heroku.
The app has a http server listening on one port (process.env.PORT) but I also want a web socket using another port. Is this possible in heroku? I am using ws.js to do the sockets.
On my local machine I can obviously use 3000 for one port and 8080 for the other, but I can't see how to define a second port that heroku will be happy to let me use.
thanks
matt

According to Heroku they don't yet support WebSockets: see this article. According to this SO question you can use socket.io (< v0.7) to do realtime stuff, but you can't use WebSockets, it'll fall back to XHR polling.

Heroku now supports websockets, as of 2013. For implementation have a look at the following article:
https://devcenter.heroku.com/articles/node-websockets

Related

How can I access Heroku NodeJS server externally?

I have a websocket server running via NodeJS, and have deployed it to Heroku. There are two separate web applications I wrote that communicate with the websocket server. On localhost, I simply run the node server, load up the applications in a web browser, and all works fine. In Heroku, however, I can't seem to get anything to connect to the url ws://url:port - it just returns an err request timeout.
I don't want to deploy my html using Express per their example. Maybe I could, but this is a test case where some web applications get to be pretty huge.
Is my issue that I have to use wss instead of ws?
Or backing up further, is this a good use case for Heroku or is there something else I should use?
The easiest description of what I'm trying to do: Two websites hosted somewhere that isn't Heroku both communicating with a NodeJS-based websocket server hosted on Heroku.
Thanks for your help!
Alright, I feel pretty dumb because this is about as easy an answer as it gets. For anyone else trying to do the same thing, you don't need to specify the port (Heroku port is for internal use only, and the heroku url you are provided is on port 80). Simply connect using wss (e.g. wss://your-app.herokuapp.com)

Is possible to React Native run a socket server?

I'd like to make a app with React-Native that's accept connections from another devices (Desktops or mobiles) through raw tcp sockets (like node's Net API) or WebSockets (like Socket.io). The point is that, socket server must be running on the React-Native's App.
I already tried Socket.io and react-native-tcp, it works when i make the server run on a nodeJS's application and the client on RN's app, but not the reverse.
When i try to import Socket.io and make it listen on a port, a error is raisen, because RN don't have node's http module. Just Socket.io/clients works.
I think that i'm doing something wrong, but is really possible to do that? and what is the best way?
Obs: I'm really new in RN's world.
No, we can't create a server although if we create a server we can't connect any other external applications to the server.
So create a server and deploy it in any could service then use it in your react-native app.

Not able to use socket io in heroku

I am new to Heroku and I am trying to deploy NodeJS application that is using socket.io to listen.
I am able to deploy that to local and its working fine. but when running the same in Heroku it is not responding.
Is there any specific port that we need to assign for socket io in Heroku?
I can't use process.env.PORT as I am using that for app server port.
Any Idea to solve this issue? or do we need to enable some config to use socket io?
I think we need 2 ports, one for normal express server and other for socket io. How to get the 2nd one.
There is no way to open more ports than 80 to the public. But WebSockets and especially socket.io should work on heroku.
Have you read these articles:
https://devcenter.heroku.com/articles/node-websockets
socket.io -> https://devcenter.heroku.com/articles/node-websockets#option-2-socket-io
http://robdodson.me/deploying-your-first-node-dot-js-and-socket-dot-io-app-to-heroku/
It's not a problem to have the websockets on port 80 because the protocol is ws:// (wss://) and it works with http:// (https://) side by side.

Cannot connect to socket.io on Google Cloud App Engine

Currently I am developing a mobile application with ionic 2. Within the application I got chat functionality. The chat functionality is based on Socket io in a NodeJS API. Unfortunality, it is hard to configure the socket io on Google Cloud App Engine. On my local machine it works fine.
Steps I have undertook:
use port 65080 (and open the firewall port)
2 servers (api calls via 8080 and socket io via 65080
Sample
Does anybody have a solution? Thank you in advance!
Jelle
Sadly, neither socket.io or Websockets currently work on App Engine. This is something we're actively trying to address:
https://cloud.google.com/appengine/docs/flexible/nodejs/how-requests-are-handled#not_supported
This question is almost identical to this one, and there's some new good news:
GAE support for persistent socket connections arrived in February 2019!
To make this work, you'll need to be using the flex environment and modify your app.yaml to include session_affinity.

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?
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."
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

Resources