React Socket IO is not connected to heroku server











up vote
0
down vote

favorite












I'm trying to get the server or port of heroku;this works fine on the localhost, but the socket.io feature doesn't work on heroku.



this is the error i get on heroku.




websocket.js:120 WebSocket connection to
'wss://localhost:5000/socket.io/?EIO=3&transport=websocket' failed:
Error in connection establishment: net::ERR_CONNECTION_REFUSED
p.doOpen @ websocket.js:120




App.js



const express = require('express');
const bodyParser = require('body-parser');
const app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var PORT = process.env.PORT || 5000;

if (process.env.NODE_ENV === 'production') {

app.use(express.static('client/build'));

app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}

io.on('connection', (socket)=>{
console.log(socket.id);

socket.on('SEND_MESSAGE', (data)=>{
io.emit('RECEIVE_MESSAGE', data);

})


});

http.listen(PORT,function(){
console.log('listening on port %s',PORT);
});


Chat.js



// this has to be something other than localhost, it needs to work vice versa.

this.socket = socketIOClient('localhost:5000');

this.socket.on('RECEIVE_MESSAGE', function(data){
addMessage(data);
});









share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to get the server or port of heroku;this works fine on the localhost, but the socket.io feature doesn't work on heroku.



    this is the error i get on heroku.




    websocket.js:120 WebSocket connection to
    'wss://localhost:5000/socket.io/?EIO=3&transport=websocket' failed:
    Error in connection establishment: net::ERR_CONNECTION_REFUSED
    p.doOpen @ websocket.js:120




    App.js



    const express = require('express');
    const bodyParser = require('body-parser');
    const app = express();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    var PORT = process.env.PORT || 5000;

    if (process.env.NODE_ENV === 'production') {

    app.use(express.static('client/build'));

    app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
    });
    }

    io.on('connection', (socket)=>{
    console.log(socket.id);

    socket.on('SEND_MESSAGE', (data)=>{
    io.emit('RECEIVE_MESSAGE', data);

    })


    });

    http.listen(PORT,function(){
    console.log('listening on port %s',PORT);
    });


    Chat.js



    // this has to be something other than localhost, it needs to work vice versa.

    this.socket = socketIOClient('localhost:5000');

    this.socket.on('RECEIVE_MESSAGE', function(data){
    addMessage(data);
    });









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to get the server or port of heroku;this works fine on the localhost, but the socket.io feature doesn't work on heroku.



      this is the error i get on heroku.




      websocket.js:120 WebSocket connection to
      'wss://localhost:5000/socket.io/?EIO=3&transport=websocket' failed:
      Error in connection establishment: net::ERR_CONNECTION_REFUSED
      p.doOpen @ websocket.js:120




      App.js



      const express = require('express');
      const bodyParser = require('body-parser');
      const app = express();
      var http = require('http').Server(app);
      var io = require('socket.io')(http);
      var PORT = process.env.PORT || 5000;

      if (process.env.NODE_ENV === 'production') {

      app.use(express.static('client/build'));

      app.get('*', (req, res) => {
      res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
      });
      }

      io.on('connection', (socket)=>{
      console.log(socket.id);

      socket.on('SEND_MESSAGE', (data)=>{
      io.emit('RECEIVE_MESSAGE', data);

      })


      });

      http.listen(PORT,function(){
      console.log('listening on port %s',PORT);
      });


      Chat.js



      // this has to be something other than localhost, it needs to work vice versa.

      this.socket = socketIOClient('localhost:5000');

      this.socket.on('RECEIVE_MESSAGE', function(data){
      addMessage(data);
      });









      share|improve this question













      I'm trying to get the server or port of heroku;this works fine on the localhost, but the socket.io feature doesn't work on heroku.



      this is the error i get on heroku.




      websocket.js:120 WebSocket connection to
      'wss://localhost:5000/socket.io/?EIO=3&transport=websocket' failed:
      Error in connection establishment: net::ERR_CONNECTION_REFUSED
      p.doOpen @ websocket.js:120




      App.js



      const express = require('express');
      const bodyParser = require('body-parser');
      const app = express();
      var http = require('http').Server(app);
      var io = require('socket.io')(http);
      var PORT = process.env.PORT || 5000;

      if (process.env.NODE_ENV === 'production') {

      app.use(express.static('client/build'));

      app.get('*', (req, res) => {
      res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
      });
      }

      io.on('connection', (socket)=>{
      console.log(socket.id);

      socket.on('SEND_MESSAGE', (data)=>{
      io.emit('RECEIVE_MESSAGE', data);

      })


      });

      http.listen(PORT,function(){
      console.log('listening on port %s',PORT);
      });


      Chat.js



      // this has to be something other than localhost, it needs to work vice versa.

      this.socket = socketIOClient('localhost:5000');

      this.socket.on('RECEIVE_MESSAGE', function(data){
      addMessage(data);
      });






      node.js sockets heroku






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 9:05









      BARNOWL

      4611419




      4611419
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          This is a hardcoded workaround



          app.js



            this.socket = socketIOClient.connect(HEROKU_PATH_HERE, {secure: true});

          if(!this.socket){
          this.socket = socketIOClient('localhost:5000');
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186306%2freact-socket-io-is-not-connected-to-heroku-server%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            This is a hardcoded workaround



            app.js



              this.socket = socketIOClient.connect(HEROKU_PATH_HERE, {secure: true});

            if(!this.socket){
            this.socket = socketIOClient('localhost:5000');
            }





            share|improve this answer

























              up vote
              0
              down vote













              This is a hardcoded workaround



              app.js



                this.socket = socketIOClient.connect(HEROKU_PATH_HERE, {secure: true});

              if(!this.socket){
              this.socket = socketIOClient('localhost:5000');
              }





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                This is a hardcoded workaround



                app.js



                  this.socket = socketIOClient.connect(HEROKU_PATH_HERE, {secure: true});

                if(!this.socket){
                this.socket = socketIOClient('localhost:5000');
                }





                share|improve this answer












                This is a hardcoded workaround



                app.js



                  this.socket = socketIOClient.connect(HEROKU_PATH_HERE, {secure: true});

                if(!this.socket){
                this.socket = socketIOClient('localhost:5000');
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 9:25









                BARNOWL

                4611419




                4611419






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186306%2freact-socket-io-is-not-connected-to-heroku-server%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini