How to import socket.io on client side - SOCKET.IO + NODE.JS












0














I have been trying to create an online game but when I try to use socket.on(...); or socket.emit(...) it comes up with an error like what is socket. I have seen some posts similar to this one and tried all the solutions but none worked. I have already got io.connect(...); working and it works (I assume this means I have properly set up socket.io-client). I just don't see what I am doing wrong. If you need the code you can simply request it though I don't think it was necessary. Thanks.










share|improve this question



























    0














    I have been trying to create an online game but when I try to use socket.on(...); or socket.emit(...) it comes up with an error like what is socket. I have seen some posts similar to this one and tried all the solutions but none worked. I have already got io.connect(...); working and it works (I assume this means I have properly set up socket.io-client). I just don't see what I am doing wrong. If you need the code you can simply request it though I don't think it was necessary. Thanks.










    share|improve this question

























      0












      0








      0







      I have been trying to create an online game but when I try to use socket.on(...); or socket.emit(...) it comes up with an error like what is socket. I have seen some posts similar to this one and tried all the solutions but none worked. I have already got io.connect(...); working and it works (I assume this means I have properly set up socket.io-client). I just don't see what I am doing wrong. If you need the code you can simply request it though I don't think it was necessary. Thanks.










      share|improve this question













      I have been trying to create an online game but when I try to use socket.on(...); or socket.emit(...) it comes up with an error like what is socket. I have seen some posts similar to this one and tried all the solutions but none worked. I have already got io.connect(...); working and it works (I assume this means I have properly set up socket.io-client). I just don't see what I am doing wrong. If you need the code you can simply request it though I don't think it was necessary. Thanks.







      javascript html node.js websocket socket.io






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 5:15









      Dragsaw Dragon

      446




      446
























          2 Answers
          2






          active

          oldest

          votes


















          1














          To use SocketIO on the client-side, first import it via



          import * as io from 'socket.io-client';


          Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server



          const socket = io(`172.16.1.3:81`);


          Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:



          socket.on("update-player", handlePlayerUpdateFunction);


          and



          function handlePlayerUpdateFunction(message) {
          console.log(`Received message from server: ${message}`);
          }


          If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.






          share|improve this answer





















          • I tried doing this but it says * is not defined...
            – Dragsaw Dragon
            Nov 13 '18 at 7:55










          • the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
            – Shawn Andrews
            Nov 13 '18 at 16:17



















          0














          After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000); my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.






          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%2f53274250%2fhow-to-import-socket-io-on-client-side-socket-io-node-js%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            To use SocketIO on the client-side, first import it via



            import * as io from 'socket.io-client';


            Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server



            const socket = io(`172.16.1.3:81`);


            Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:



            socket.on("update-player", handlePlayerUpdateFunction);


            and



            function handlePlayerUpdateFunction(message) {
            console.log(`Received message from server: ${message}`);
            }


            If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.






            share|improve this answer





















            • I tried doing this but it says * is not defined...
              – Dragsaw Dragon
              Nov 13 '18 at 7:55










            • the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
              – Shawn Andrews
              Nov 13 '18 at 16:17
















            1














            To use SocketIO on the client-side, first import it via



            import * as io from 'socket.io-client';


            Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server



            const socket = io(`172.16.1.3:81`);


            Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:



            socket.on("update-player", handlePlayerUpdateFunction);


            and



            function handlePlayerUpdateFunction(message) {
            console.log(`Received message from server: ${message}`);
            }


            If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.






            share|improve this answer





















            • I tried doing this but it says * is not defined...
              – Dragsaw Dragon
              Nov 13 '18 at 7:55










            • the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
              – Shawn Andrews
              Nov 13 '18 at 16:17














            1












            1








            1






            To use SocketIO on the client-side, first import it via



            import * as io from 'socket.io-client';


            Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server



            const socket = io(`172.16.1.3:81`);


            Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:



            socket.on("update-player", handlePlayerUpdateFunction);


            and



            function handlePlayerUpdateFunction(message) {
            console.log(`Received message from server: ${message}`);
            }


            If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.






            share|improve this answer












            To use SocketIO on the client-side, first import it via



            import * as io from 'socket.io-client';


            Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server



            const socket = io(`172.16.1.3:81`);


            Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:



            socket.on("update-player", handlePlayerUpdateFunction);


            and



            function handlePlayerUpdateFunction(message) {
            console.log(`Received message from server: ${message}`);
            }


            If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 5:30









            Shawn Andrews

            945617




            945617












            • I tried doing this but it says * is not defined...
              – Dragsaw Dragon
              Nov 13 '18 at 7:55










            • the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
              – Shawn Andrews
              Nov 13 '18 at 16:17


















            • I tried doing this but it says * is not defined...
              – Dragsaw Dragon
              Nov 13 '18 at 7:55










            • the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
              – Shawn Andrews
              Nov 13 '18 at 16:17
















            I tried doing this but it says * is not defined...
            – Dragsaw Dragon
            Nov 13 '18 at 7:55




            I tried doing this but it says * is not defined...
            – Dragsaw Dragon
            Nov 13 '18 at 7:55












            the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
            – Shawn Andrews
            Nov 13 '18 at 16:17




            the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
            – Shawn Andrews
            Nov 13 '18 at 16:17













            0














            After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000); my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.






            share|improve this answer


























              0














              After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000); my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.






              share|improve this answer
























                0












                0








                0






                After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000); my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.






                share|improve this answer












                After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000); my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 23:46









                Dragsaw Dragon

                446




                446






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53274250%2fhow-to-import-socket-io-on-client-side-socket-io-node-js%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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python