I'm trying to deploy my Node.js, Express.js, Socket.IO and React based application onto Heroku.
But on the client side, the socket.io-client always seems to connect to localhost even though I have never specified localhost while connecting.
I get this error in the browser console:
bundle.js:208 Mixed Content: The page at 'https://test.herokuapp.com/#/test?_k=v7l28t' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LLobEYP'. This request has been blocked; the content must be served over HTTPS.
If I try to open it on http I get a connection refused error:
bundle.js:208 GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LLpAsLm net::ERR_CONNECTION_REFUSED
Here's what my server socket looks like:
var express = require('express');
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
server.listen(port);
io.on('connection', function(socket) {
socket.on('news', function(message) {
console.log("Responding with news");
io.sockets.emit('news', 'news');
});
});
And here's the client code:
var io = require('socket.io-client');
var socket = io.connect();
socket.on('connect', function() {
console.log('connected to server');
socket.emit('news', 'news');
});
Plus the package.json:
{
"name": "test-socket",
"version": "1.0.0",
"private": true,
"scripts": {
"dev-server": "nodemon ./bin/www",
"dev-client": "watchify -t [ envify --NOVE_ENV development ] -t [ babelify --presets [ react es2015 ] ] public/javascripts/index.js -o public/javascripts/build/bundle.js -v",
"dev": "concurrently \"npm run dev-server\" \"npm run dev-client\"",
"build": "browserify -t [ envify --NOVE_ENV production ] -t [ babelify --presets [ react es2015 ] ] -g uglifyify public/javascripts/index.js -o public/javascripts/build/bundle.js",
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"fakeredis": "^1.0.3",
"jade": "~1.11.0",
"lodash": "^4.11.1",
"material-ui": "^0.15.0-beta.2",
"mongoose": "^4.5.0",
"morgan": "~1.6.1",
"passport": "^0.3.2",
"passport-http": "^0.3.0",
"passport-local": "^1.0.0",
"react": "^15.0.1",
"react-bootstrap": "^0.28.5",
"react-dom": "^15.0.1",
"react-router": "^2.4.1",
"react-tap-event-plugin": "^1.0.0",
"redis": "^2.6.2",
"serve-favicon": "~2.3.0",
"socket.io": "^1.4.6",
"socket.io-client": "^1.4.6",
"whatwg-fetch": "^0.11.0"
},
"devDependencies": {
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babelify": "^7.2.0",
"browserify": "^13.0.0",
"concurrently": "^2.1.0",
"envify": "^3.4.0",
"nodemon": "^1.9.2",
"uglifyify": "^3.0.1",
"watchify": "^3.7.0"
},
"description": "",
"main": "app.js",
"author": "Harsha Bhat",
"license": "ISC"
}
Kindly let me know if I'm doing something wrong here. Appreciate your help :)
This works for me if I use for client:
const socketURL =
process.env.NODE_ENV === 'production'
? window.location.hostname
: 'https://localhost:5000';
const socket = io.connect(socketURL, {secure: true});
Your website runs on https, but socket on http. Use a secure URL for your initial connection, ie instead of "http://"
use "https://"
var socket = io.connect('https://test.herokuapp.com', {secure: true});
Or open your website at http://test.herokuapp.com (without ssl)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.