Socket.io is not working on heroku server, probably a problem with ports

Asked
Active3 hr before
Viewed126 times

7 Answers

socketherokuprobablyproblemportsserverworking
90%

So, basically, remove all the occurrences of static ports and connect socket.io & node server to the same port.,So, after reading several blogs and StackOverflow posts I was able to find a solution to this problem. Hope this helps anyone facing a similar issue.,Here, just remove the static localhost URL/port., Does a long vertical pole fall at a different speed than a short vertical pole?

So, basically, remove all the occurrences of static ports and connect socket.io & node server to the same port.

const express = require('express');
const http = require('http');
const cors = require('cors');
const path = require('path');
const dataBase = require('./config/db');

const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server);

const authRoutes = require('./routes/auth');
const managerRoutes = require('./routes/manager');
const leadRoutes = require('./routes/lead');
const resourceRoutes = require('./routes/resource');

dataBase();

app.use(express.json());
app.use(cors());

const port = process.env.PORT || 5000;

if (process.env.NODE_ENV === 'production') {
   app.use(express.static(path.join(__dirname, 'client/build')));

   app.get('*', function(req, res) {
      res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
   });
}

io.on('connection', (socket) => {
         socket.on('join', async ({
            userId
         }, callback) => {
            ......some code here
         });

         server.listen(port, (error) => {
            if (error) console.log(error);
            console.log(`Server started on port ${port}`);
         });
load more v
88%

Continuous Integration , Deployment Integrations , Deploying with Git , Deploying with Docker

Move into your app’s directory and create a default package.json:

$ npm init--yes
load more v
72%

Next we need to cd into the defcon folder so we can install Socket.io and our other dependencies. There's a version of Socket.io designed to work with Express so we'll install that one.,Now that you have Express installed you can use it to create a new project.,We'll need to scale our web process before we can use the app.,I guess that's just a habit of using Backbone Boilerplate all the time :) Here's what main.js should look like:

To start us off we'll use Express since it provides a nice, Sinatra like layer on top of Node. If you haven't installed Node yet you can do it from the Node.js site. The installer will also include npm which is Node's package manager. Following the instructions on the Express site you should be able to just type:

npm install - g express
load more v
65%

I still get the H10 error, but it's working locally. Maybe the flask app, under the hood, isn't being passed the appropriate port?,i too added the application to heroku which uses flask socketio The whole app is in my repository, LivelyZone,Looks like npm's default .gitignore included the build folder so the index.html wasn't getting pushed. I really appreciate all the help @miguelgrinberg đź‘Ť,it's working now. nice.

anyjson == 0.3 .3
beautifulsoup4 == 4.4 .1
billiard == 3.3 .0 .21
blinker == 1.4
celery == 3.1 .19
dominate == 2.1 .16
eventlet == 0.17 .4
Flask == 0.10 .1
Flask - Admin == 1.3 .0
Flask - Bootstrap == 3.3 .5 .6
Flask - Login == 0.3 .1
Flask - Mail == 0.9 .1
flask - mongoengine == 0.7 .1
Flask - Principal == 0.4 .0
Flask - PyMongo == 0.3 .1
Flask - Security == 1.7 .4
Flask - SocketIO == 1.1
Flask - SQLAlchemy == 2.0
Flask - WTF == 0.12
greenlet == 0.4 .9
gunicorn == 18.0 .0
itsdangerous == 0.24
Jinja2 == 2.8
kombu == 3.0 .29
MarkupSafe == 0.23
mongoengine == 0.10 .0
passlib == 1.6 .5
pep8 == 1.6 .2
pymongo == 2.9
python - engineio == 0.8 .0
python - socketio == 0.7 .0
pytz == 2015.7
redis == 2.10 .5
requests == 2.8 .0
six == 1.10 .0
SQLAlchemy == 1.0 .8
Werkzeug == 0.10 .4
wheel == 0.24 .0
WTForms == 2.0 .2
load more v
75%

If your app uses sockets and you're using nginx, be sure to configure it to relay websocket messages to your server. You can find guidance on proxying WebSockets in nginx's docs on the subject.,You probably don't need to worry about setting your app to run on port 80 (if not behind a proxy like nginx). Most PaaS providers automatically figure out the port for you. If you are using a proxy server, please refer to its documentation (whether or not you need to configure the port for your Sails app depends on how you set things up and can vary widely based on your needs).,As mentioned above, ignore this step if your app will be running behind a load balancer or proxy.,When your app is running in a production environment:

Whether it's by using the sails_port environment variable, setting the --port command-line option, or changing your production config file(s), add the following to the top level of your Sails config:

port: 80
load more v
40%

Make sure you are not running slow commands that are blocking the server. Use the Redis Slow Log feature to check this.,The following documentation is very important in order to run Redis in a low latency fashion. However I understand that we are busy people, so let's start with a quick checklist. If you fail following these steps, please return here to read the full documentation.,Make sure to disable transparent huge pages using the following command:,To enable the feature just use the following:

If you are experiencing latency problems, you probably know how to measure it in the context of your application, or maybe your latency problem is very evident even macroscopically. However redis-cli can be used to measure the latency of a Redis server in milliseconds, just try:

redis - cli--latency - h `host` - p `port`
load more v
22%

nodemon or supervisor for automatic restart,The source code for the example is the following:,In this article we will talk about the most common mistakes Node developers make and how to avoid them. You can find the source code for the examples on github.,1 Not using development tools 1.1 Automating restarts 1.2 Automatic browser refresh

What these modules do is to watch for file changes and restart the server for you. Let us take nodemon for example. First you install it globally:

  npm i nodemon - g
load more v

Other "socket-heroku" queries related to "Socket.io is not working on heroku server, probably a problem with ports"