Socket.io woks with localhost but not on Heroku server

I am currently trying to use socket.io and the node.js server to communicate with a Unity script. Everything is connected to me and works with localhost, but for some reason, when I transfer it to my Heroku server, it cannot connect. I assume this may have something to do with the url? I am new to socket.io, so any help would be appreciated.

My node.js server:

var express = require('express'); var app = express(); var expressWs = require('express-ws')(app); var path = require('path'); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(socket) { socket.on('beep', function(){ socket.emit("speed", {data: 5}); console.log('beep recieved'); }); socket.on('change-speed', function(data) { console.log('change speed recieved: ' + data); socket.emit("speed", {newSpeed: data}); }); socket.on('ios-connection', function(data) { console.log('ios connection with message: ' + data); }); }); app.set('port', (process.env.PORT || 5000)); app.listen(app.get('port'), function() { console.log('Node app is running on port', app.get('port')); }); 

My connection url is:

 ws://<heroku app name>.herokuapp.com:5000/socket.io/?EIO=4&transport=websocket 
+5
source share
4 answers

The problem is almost certainly the wrong port number.

In your application, you check the value of process.env.PORT , and if it is not installed, you use 5000 by default.

However, in your ws url, you always expect the application to listen on port 5000.

You can check the configuration settings of your application by running the following command in the root directory of your project:

heroku run printenv

This will print a list of configuration vars, including the current PORT value, for example:

PORT=9352

This port should be used when creating ws URLs, for example:

WS: //your-app.herokuapp.com: 9352 / socket.io / EIO = 4 & transport = WebSocket

+3
source

I deployed your code with a few changes, and it works great on the hero, please take a look at it. Server side app.js

 var express = require('express'); var app = express(); app.set('port', (process.env.PORT || 5000)); var server = app.listen(app.get('port'), function() { console.log('Node app is running on port', app.get('port')); }); var io = require('socket.io')(server); app.use(express.static("./views")); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); next(); }); app.get('/', function (req, res) { var path = __dirname + '/views/index.html'; console.log(path); res.sendFile(path); }); io.on('connection', function(socket) { socket.on('beep', function(){ socket.emit("beep", {data: 5}); console.log('beep recieved'); }); socket.on('change-speed', function(data) { console.log('change speed recieved: ' + data); socket.emit("speed", {newSpeed: data}); }); socket.on('ios-connection', function(data) { console.log('ios connection with message: ' + data); }); }); 

package.json

 { "name": "socketio", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start" : "node app.js" }, "author": "inampaki", "license": "ISC", "dependencies": { "express": "^4.13.3", "express-ws": "^0.2.6", "socket.io": "^1.3.7" } } 

index.html

 <script src="/socket.io.js"></script> <script> var socket = io.connect('/'); socket.on('speed', function (data) { console.log('speed Message Received!'); console.log(data); }); socket.on('beep', function (data) { console.log('beep Message Received!'); console.log(data); }); socket.emit("beep", {beep : true}); socket.emit("change-speed", {"change-speed" : true}); socket.emit("ios-connection", {"ios-connection" : true}); </script> 

Note that save index.html and socket.io.js in the views folder. URL where I deployed it, socketip

+1
source

Let me tell you what I did as a solution. I used this project: https://github.com/teyou/Unity_POC_Socket which works with Heroku And changed the io socket code to my code and it works ... So you can do this or check what this project forces work.

+1
source

I found a way !!. In Unity

if you start the server in the local host. url should have ": port" example (port = 5000)

 ws://127.0.0.1:5000/socket.io/?EIO=4&transport=websocket 

but if you deployed to ** heroku url should remove ": port"

 ws://<heroku app name>.herokuapp.com/socket.io/?EIO=4&transport=websocket 

It works for me!

0
source

Source: https://habr.com/ru/post/1239368/


All Articles