JS + Socket.io + Heroku Problems - Socket.id problem












0

















Solution



Change



var server = app.listen(3000);


To



var server = app.listen(process.env.PORT || 5000);




I want to deploy an game made with JavaScript on Heroku.



Here's my server:



var express = require('express');
var app = express();
var server = app.listen(3000);
var socket = require('socket.io');
var io = socket(server);
app.use(express.static('public'));

var connectedPlayers = {};

console.log("Server is running!");

io.on('connection',

function (socket) {

socket.on('newPlayer',
function (data) {
console.log("New player connected - ID: " + data.id);
connectedPlayers[socket.id] = {
idOnline: socket.id,
idOffline: data.id,
x: data.w,
y: data.h
};
socket.emit('allPlayers', connectedPlayers);
socket.broadcast.emit('newPlayer', connectedPlayers[socket.id]);
});

socket.on('move',
function (data) {
connectedPlayers[socket.id].x = data.x;
connectedPlayers[socket.id].y = data.y;
socket.broadcast.emit('move', connectedPlayers[socket.id]);
});

socket.on('message',
function (data) {
message = {
name: data.name,
message: data.message,
messageId: generateId()
};
socket.broadcast.emit('message', message);
});

socket.on('emote',
function (data) {
message = {
emote: data.emote,
id: socket.id
}
socket.broadcast.emit('emote', message);
});

socket.on('disconnect', function () {
delete connectedPlayers[socket.id];
io.emit('remove', socket.id);
});

});


This work's fine locally, but when I deploy to heroku I get this error message:



2018-11-23T21:04:18.009491+00:00 app[web.1]: /app/server.js:33
2018-11-23T21:04:18.009512+00:00 app[web.1]: connectedPlayers[socket.id].x = data.x;
2018-11-23T21:04:18.009514+00:00 app[web.1]: ^
2018-11-23T21:04:18.009516+00:00 app[web.1]:
2018-11-23T21:04:18.009518+00:00 app[web.1]: TypeError: Cannot set property 'x' of undefined


I understand that heroku is not recognizing the "connectedPlayers" array at that index, but how this can work properly locally?
What's wrong with the socket.id property?
PS.: the socket.id it's sended by the client, but I think that's generated after a client establish an connection with the server right?










share|improve this question





























    0

















    Solution



    Change



    var server = app.listen(3000);


    To



    var server = app.listen(process.env.PORT || 5000);




    I want to deploy an game made with JavaScript on Heroku.



    Here's my server:



    var express = require('express');
    var app = express();
    var server = app.listen(3000);
    var socket = require('socket.io');
    var io = socket(server);
    app.use(express.static('public'));

    var connectedPlayers = {};

    console.log("Server is running!");

    io.on('connection',

    function (socket) {

    socket.on('newPlayer',
    function (data) {
    console.log("New player connected - ID: " + data.id);
    connectedPlayers[socket.id] = {
    idOnline: socket.id,
    idOffline: data.id,
    x: data.w,
    y: data.h
    };
    socket.emit('allPlayers', connectedPlayers);
    socket.broadcast.emit('newPlayer', connectedPlayers[socket.id]);
    });

    socket.on('move',
    function (data) {
    connectedPlayers[socket.id].x = data.x;
    connectedPlayers[socket.id].y = data.y;
    socket.broadcast.emit('move', connectedPlayers[socket.id]);
    });

    socket.on('message',
    function (data) {
    message = {
    name: data.name,
    message: data.message,
    messageId: generateId()
    };
    socket.broadcast.emit('message', message);
    });

    socket.on('emote',
    function (data) {
    message = {
    emote: data.emote,
    id: socket.id
    }
    socket.broadcast.emit('emote', message);
    });

    socket.on('disconnect', function () {
    delete connectedPlayers[socket.id];
    io.emit('remove', socket.id);
    });

    });


    This work's fine locally, but when I deploy to heroku I get this error message:



    2018-11-23T21:04:18.009491+00:00 app[web.1]: /app/server.js:33
    2018-11-23T21:04:18.009512+00:00 app[web.1]: connectedPlayers[socket.id].x = data.x;
    2018-11-23T21:04:18.009514+00:00 app[web.1]: ^
    2018-11-23T21:04:18.009516+00:00 app[web.1]:
    2018-11-23T21:04:18.009518+00:00 app[web.1]: TypeError: Cannot set property 'x' of undefined


    I understand that heroku is not recognizing the "connectedPlayers" array at that index, but how this can work properly locally?
    What's wrong with the socket.id property?
    PS.: the socket.id it's sended by the client, but I think that's generated after a client establish an connection with the server right?










    share|improve this question



























      0












      0








      0










      Solution



      Change



      var server = app.listen(3000);


      To



      var server = app.listen(process.env.PORT || 5000);




      I want to deploy an game made with JavaScript on Heroku.



      Here's my server:



      var express = require('express');
      var app = express();
      var server = app.listen(3000);
      var socket = require('socket.io');
      var io = socket(server);
      app.use(express.static('public'));

      var connectedPlayers = {};

      console.log("Server is running!");

      io.on('connection',

      function (socket) {

      socket.on('newPlayer',
      function (data) {
      console.log("New player connected - ID: " + data.id);
      connectedPlayers[socket.id] = {
      idOnline: socket.id,
      idOffline: data.id,
      x: data.w,
      y: data.h
      };
      socket.emit('allPlayers', connectedPlayers);
      socket.broadcast.emit('newPlayer', connectedPlayers[socket.id]);
      });

      socket.on('move',
      function (data) {
      connectedPlayers[socket.id].x = data.x;
      connectedPlayers[socket.id].y = data.y;
      socket.broadcast.emit('move', connectedPlayers[socket.id]);
      });

      socket.on('message',
      function (data) {
      message = {
      name: data.name,
      message: data.message,
      messageId: generateId()
      };
      socket.broadcast.emit('message', message);
      });

      socket.on('emote',
      function (data) {
      message = {
      emote: data.emote,
      id: socket.id
      }
      socket.broadcast.emit('emote', message);
      });

      socket.on('disconnect', function () {
      delete connectedPlayers[socket.id];
      io.emit('remove', socket.id);
      });

      });


      This work's fine locally, but when I deploy to heroku I get this error message:



      2018-11-23T21:04:18.009491+00:00 app[web.1]: /app/server.js:33
      2018-11-23T21:04:18.009512+00:00 app[web.1]: connectedPlayers[socket.id].x = data.x;
      2018-11-23T21:04:18.009514+00:00 app[web.1]: ^
      2018-11-23T21:04:18.009516+00:00 app[web.1]:
      2018-11-23T21:04:18.009518+00:00 app[web.1]: TypeError: Cannot set property 'x' of undefined


      I understand that heroku is not recognizing the "connectedPlayers" array at that index, but how this can work properly locally?
      What's wrong with the socket.id property?
      PS.: the socket.id it's sended by the client, but I think that's generated after a client establish an connection with the server right?










      share|improve this question


















      Solution



      Change



      var server = app.listen(3000);


      To



      var server = app.listen(process.env.PORT || 5000);




      I want to deploy an game made with JavaScript on Heroku.



      Here's my server:



      var express = require('express');
      var app = express();
      var server = app.listen(3000);
      var socket = require('socket.io');
      var io = socket(server);
      app.use(express.static('public'));

      var connectedPlayers = {};

      console.log("Server is running!");

      io.on('connection',

      function (socket) {

      socket.on('newPlayer',
      function (data) {
      console.log("New player connected - ID: " + data.id);
      connectedPlayers[socket.id] = {
      idOnline: socket.id,
      idOffline: data.id,
      x: data.w,
      y: data.h
      };
      socket.emit('allPlayers', connectedPlayers);
      socket.broadcast.emit('newPlayer', connectedPlayers[socket.id]);
      });

      socket.on('move',
      function (data) {
      connectedPlayers[socket.id].x = data.x;
      connectedPlayers[socket.id].y = data.y;
      socket.broadcast.emit('move', connectedPlayers[socket.id]);
      });

      socket.on('message',
      function (data) {
      message = {
      name: data.name,
      message: data.message,
      messageId: generateId()
      };
      socket.broadcast.emit('message', message);
      });

      socket.on('emote',
      function (data) {
      message = {
      emote: data.emote,
      id: socket.id
      }
      socket.broadcast.emit('emote', message);
      });

      socket.on('disconnect', function () {
      delete connectedPlayers[socket.id];
      io.emit('remove', socket.id);
      });

      });


      This work's fine locally, but when I deploy to heroku I get this error message:



      2018-11-23T21:04:18.009491+00:00 app[web.1]: /app/server.js:33
      2018-11-23T21:04:18.009512+00:00 app[web.1]: connectedPlayers[socket.id].x = data.x;
      2018-11-23T21:04:18.009514+00:00 app[web.1]: ^
      2018-11-23T21:04:18.009516+00:00 app[web.1]:
      2018-11-23T21:04:18.009518+00:00 app[web.1]: TypeError: Cannot set property 'x' of undefined


      I understand that heroku is not recognizing the "connectedPlayers" array at that index, but how this can work properly locally?
      What's wrong with the socket.id property?
      PS.: the socket.id it's sended by the client, but I think that's generated after a client establish an connection with the server right?







      javascript heroku socket.io






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 21:48







      Jorge Nachtigall

















      asked Nov 23 '18 at 21:31









      Jorge NachtigallJorge Nachtigall

      154




      154
























          1 Answer
          1






          active

          oldest

          votes


















          0














          After looking for a lot of solutions, I was trying crazy things to insert the:



          process.env.PORT || 5000


          on the server.listen.



          As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):



          var server = app.listen(3000);


          The solution was simplier than I thought, just changing the above line of code to:



          var server = app.listen(process.env.PORT || 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',
            autoActivateHeartbeat: false,
            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%2f53453144%2fjs-socket-io-heroku-problems-socket-id-problem%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









            0














            After looking for a lot of solutions, I was trying crazy things to insert the:



            process.env.PORT || 5000


            on the server.listen.



            As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):



            var server = app.listen(3000);


            The solution was simplier than I thought, just changing the above line of code to:



            var server = app.listen(process.env.PORT || 5000);





            share|improve this answer




























              0














              After looking for a lot of solutions, I was trying crazy things to insert the:



              process.env.PORT || 5000


              on the server.listen.



              As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):



              var server = app.listen(3000);


              The solution was simplier than I thought, just changing the above line of code to:



              var server = app.listen(process.env.PORT || 5000);





              share|improve this answer


























                0












                0








                0







                After looking for a lot of solutions, I was trying crazy things to insert the:



                process.env.PORT || 5000


                on the server.listen.



                As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):



                var server = app.listen(3000);


                The solution was simplier than I thought, just changing the above line of code to:



                var server = app.listen(process.env.PORT || 5000);





                share|improve this answer













                After looking for a lot of solutions, I was trying crazy things to insert the:



                process.env.PORT || 5000


                on the server.listen.



                As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):



                var server = app.listen(3000);


                The solution was simplier than I thought, just changing the above line of code to:



                var server = app.listen(process.env.PORT || 5000);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 21:45









                Jorge NachtigallJorge Nachtigall

                154




                154
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453144%2fjs-socket-io-heroku-problems-socket-id-problem%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







                    Popular posts from this blog

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga