Not able to use socket io in heroku - node.js

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.

Related

Beginner question about Ports and server listening

I've just completed a couple of Node.js tutorials and would like a bit of clarification about server port listening and communication between computers in general.
I've created a couple of basic Node.js servers, and what I've learned in that you have to tell your server to listen on a certain port. My questions are as follows:
Say my computer (PC1) is listening on port 3000, does that mean when a client (say PC2) is trying to connect to my server through the internet, the client must be sending their request via port 3000 on their side for my server to receive and respond to the request?
Following on from Q1 - And if PC2 (client) is trying to connect to PC1 (server) and the client's port is different to what the server is listening for, does that mean nothing happens?
This is a very "beginnerish" question. Say I've got a basic Node.js server up and running, and a client makes a request. Before the client information even reaches the server application running in Node.js, a connection between the client and server through the internet must first be established through their IP addresses, right? Then after that, the server application will respond if the port it's listening on is the same port that the client sent the request?
I realise these are basic questions, but I'd really like to firmly grasp these concepts before moving forward with my backend adventure.
Feel free to direct me to any resources you think would help me understand these concepts better.
Many thanks!
If your firewall is off, and you are behind a router that forwards the port 3000 to you, then yes, your 3000 is open and you can connect freely from your publicIP:3000.
What we do normally, we listen on localhost:3000, but we firewall 3000. Then we have a frontend reverse proxy (nginx, HAProxy), that reverses port 80(http)/443(https) to 3000.
Reverse proxy allows listening to different domains as well, so you can have a app on 3000, a app on 3001 for different apps, and redirect different domain to it.
If you cannot access from PUBLIC IP because you are behind a local area network, then you can install something like ngrok which allows you to tunnel your connection from the internet.

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.

SocketIO on port 80 together with Express

I have ExspressJS app run with Socket.io, due to firewall issues with higher port for SIO i want to switch that both will work on port 80.
Found this small article and on my dev machine it's look working good.
My question is, is it really goo to do that? is it a good practice? if not why?
Please advise.
It makes absolute sense to run socket.io and your web server on the same port.
The webSocket protocol (which socket.io is based on) is specifically designed for this to be the primary way that socket.io is used for a bunch of reasons including same-origin permissions and client and server firewall routing of port 80.
In case you didn't realize it, every socket.io connection starts with an HTTP request to a specific route and then once the initial handshake between client and server has been confirmed, then the protocol is "upgraded" from HTTP to webSocket. Because all socket.io connections connect in on a very specific route, all other HTTP connections can easily be separated out and be treated by your web server as regular web requests.

socket.io) Is it okay to show port numbers in client side?

I'm kinda new to socket.io and node.js.
I currently set up my server with nginx as reverse proxy.
Regular requests are going to port 8080, apache server.
Some specific requests will go to port 8888, node.js server.
I created a simple chat using socket.io and node.js.
Everything works fine, but since node.js server is listening to port 8888..
I have codes on client as
<script src='myserver:8888/socket.io/socket.io.js'></script>
and
var socket = io.connect('myserver:8888');
Is it okay to write like that?
Or is there any other way I can prevent writing port numbers directly?
You may want to proxy request using nginx to you node.js server. That way you can have your code on client as
<script src='node.myserver/socket.io/socket.io.js'></script>
instead of having to write port number.
look in to this tutorial
https://chrislea.com/2013/02/23/proxying-websockets-with-nginx/

Node.js on Heroku with 2 ports

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

Resources