How to spin up a websocket server paralelly along with express app?NodeJS WebSocket Handshake Silently Failing?How to get the full url in Express?How to access the GET parameters after “?” in Express?Which WebSocket library to use in Android app?express-ws how to periodically check a custom event and take action automaticallyArchitecture with http server, websocket and expressWebSocket connection closes when server sends message only on port 80Node.js WebSockets - send data to server with BasicAuthUsing an Express Server along with a Websocket Server; how can I trigger a Websocket send event after a POST to one of my routes?call require not possible in other file .js for nodeJs and express project

Can you cover a cube with copies of this shape?

Co-worker is now managing my team. Does this mean that I'm being demoted?

Is this set open or closed (or both?)

At what temperature should the earth be cooked to prevent human infection?

...and then she held the gun

Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?

How can the US president give an order to a civilian?

Why do you need to heat the pan before heating the olive oil?

High-end PC graphics circa 1990?

Can I drive in EU states and Switzerland with German proof of a surrendered U.S. license?

What kind of chart is this?

Background for black and white chart

Is there a term for someone whose preferred policies are a mix of Left and Right?

what is "dot" sign in the •NO?

Why are almost all the people in this orchestra recording wearing headphones with one ear on and one ear off?

Why is Skinner so awkward in Hot Fuzz?

Does knowing the surface area of all faces uniquely determine a tetrahedron?

Why can't we feel the Earth's revolution?

How do I run a script as sudo at boot time on Ubuntu 18.04 Server?

On George Box, Galit Shmueli and the scientific method?

What is this plant I saw for sale at a Romanian farmer's market?

How to prevent cables getting intertwined

Is it a bad idea to have a pen name with only an initial for a surname?

Leaving job close to major deadlines



How to spin up a websocket server paralelly along with express app?


NodeJS WebSocket Handshake Silently Failing?How to get the full url in Express?How to access the GET parameters after “?” in Express?Which WebSocket library to use in Android app?express-ws how to periodically check a custom event and take action automaticallyArchitecture with http server, websocket and expressWebSocket connection closes when server sends message only on port 80Node.js WebSockets - send data to server with BasicAuthUsing an Express Server along with a Websocket Server; how can I trigger a Websocket send event after a POST to one of my routes?call require not possible in other file .js for nodeJs and express project






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have an express application, and a websocket script.
I create the server and spinup the express app in a www.js file.



The problem I'm facing is that, I'm not able to instantiate the websocket inside the www.js file.



Here is the www.js



....
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
....


Here is the websocket.js



const WebSocket = require('ws');

const wss = new WebSocket.Server( port: 8080 );

wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);

ws.send('something');
);


I know the line const wss = new Websocket.Server( port: 8080 ); creates the websocket instance...but I want to be able to create this instance in www.js file and all the logic for websocket in a seperate file.










share|improve this question






























    1















    I have an express application, and a websocket script.
    I create the server and spinup the express app in a www.js file.



    The problem I'm facing is that, I'm not able to instantiate the websocket inside the www.js file.



    Here is the www.js



    ....
    const server = http.createServer(app);
    server.listen(port);
    server.on("error", onError);
    server.on("listening", onListening);
    ....


    Here is the websocket.js



    const WebSocket = require('ws');

    const wss = new WebSocket.Server( port: 8080 );

    wss.on('connection', function connection(ws)
    ws.on('message', function incoming(message)
    console.log('received: %s', message);
    );

    ws.send('something');
    );


    I know the line const wss = new Websocket.Server( port: 8080 ); creates the websocket instance...but I want to be able to create this instance in www.js file and all the logic for websocket in a seperate file.










    share|improve this question


























      1












      1








      1








      I have an express application, and a websocket script.
      I create the server and spinup the express app in a www.js file.



      The problem I'm facing is that, I'm not able to instantiate the websocket inside the www.js file.



      Here is the www.js



      ....
      const server = http.createServer(app);
      server.listen(port);
      server.on("error", onError);
      server.on("listening", onListening);
      ....


      Here is the websocket.js



      const WebSocket = require('ws');

      const wss = new WebSocket.Server( port: 8080 );

      wss.on('connection', function connection(ws)
      ws.on('message', function incoming(message)
      console.log('received: %s', message);
      );

      ws.send('something');
      );


      I know the line const wss = new Websocket.Server( port: 8080 ); creates the websocket instance...but I want to be able to create this instance in www.js file and all the logic for websocket in a seperate file.










      share|improve this question
















      I have an express application, and a websocket script.
      I create the server and spinup the express app in a www.js file.



      The problem I'm facing is that, I'm not able to instantiate the websocket inside the www.js file.



      Here is the www.js



      ....
      const server = http.createServer(app);
      server.listen(port);
      server.on("error", onError);
      server.on("listening", onListening);
      ....


      Here is the websocket.js



      const WebSocket = require('ws');

      const wss = new WebSocket.Server( port: 8080 );

      wss.on('connection', function connection(ws)
      ws.on('message', function incoming(message)
      console.log('received: %s', message);
      );

      ws.send('something');
      );


      I know the line const wss = new Websocket.Server( port: 8080 ); creates the websocket instance...but I want to be able to create this instance in www.js file and all the logic for websocket in a seperate file.







      node.js express ws






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 5:11







      Goutam B Seervi

















      asked Mar 25 at 3:59









      Goutam B SeerviGoutam B Seervi

      4917




      4917






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Pass the http server instance to the websocket server,



          const wss = new Websocket.Server( server: server );



          If you want to separate it as a single modules to a file,



          const WebSocket = require('ws');


          const initWSServer = (server) =>
          const wss = new WebSocket.Server( server: server);

          wss.on('connection', function connection(ws)

          ws.on('message', function incoming(message)
          console.log('received: %s', message);
          );

          ws.send('something');

          );


          module.exports =
          initWSServer



          Then,



          const server = http.createServer(app);
          server.listen(port);
          server.on("error", onError);
          server.on("listening", onListening);

          const ws_server = require('./websocket.js');
          ws_server.initWSServer(server);





          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%2f55331093%2fhow-to-spin-up-a-websocket-server-paralelly-along-with-express-app%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









            1














            Pass the http server instance to the websocket server,



            const wss = new Websocket.Server( server: server );



            If you want to separate it as a single modules to a file,



            const WebSocket = require('ws');


            const initWSServer = (server) =>
            const wss = new WebSocket.Server( server: server);

            wss.on('connection', function connection(ws)

            ws.on('message', function incoming(message)
            console.log('received: %s', message);
            );

            ws.send('something');

            );


            module.exports =
            initWSServer



            Then,



            const server = http.createServer(app);
            server.listen(port);
            server.on("error", onError);
            server.on("listening", onListening);

            const ws_server = require('./websocket.js');
            ws_server.initWSServer(server);





            share|improve this answer





























              1














              Pass the http server instance to the websocket server,



              const wss = new Websocket.Server( server: server );



              If you want to separate it as a single modules to a file,



              const WebSocket = require('ws');


              const initWSServer = (server) =>
              const wss = new WebSocket.Server( server: server);

              wss.on('connection', function connection(ws)

              ws.on('message', function incoming(message)
              console.log('received: %s', message);
              );

              ws.send('something');

              );


              module.exports =
              initWSServer



              Then,



              const server = http.createServer(app);
              server.listen(port);
              server.on("error", onError);
              server.on("listening", onListening);

              const ws_server = require('./websocket.js');
              ws_server.initWSServer(server);





              share|improve this answer



























                1












                1








                1







                Pass the http server instance to the websocket server,



                const wss = new Websocket.Server( server: server );



                If you want to separate it as a single modules to a file,



                const WebSocket = require('ws');


                const initWSServer = (server) =>
                const wss = new WebSocket.Server( server: server);

                wss.on('connection', function connection(ws)

                ws.on('message', function incoming(message)
                console.log('received: %s', message);
                );

                ws.send('something');

                );


                module.exports =
                initWSServer



                Then,



                const server = http.createServer(app);
                server.listen(port);
                server.on("error", onError);
                server.on("listening", onListening);

                const ws_server = require('./websocket.js');
                ws_server.initWSServer(server);





                share|improve this answer















                Pass the http server instance to the websocket server,



                const wss = new Websocket.Server( server: server );



                If you want to separate it as a single modules to a file,



                const WebSocket = require('ws');


                const initWSServer = (server) =>
                const wss = new WebSocket.Server( server: server);

                wss.on('connection', function connection(ws)

                ws.on('message', function incoming(message)
                console.log('received: %s', message);
                );

                ws.send('something');

                );


                module.exports =
                initWSServer



                Then,



                const server = http.createServer(app);
                server.listen(port);
                server.on("error", onError);
                server.on("listening", onListening);

                const ws_server = require('./websocket.js');
                ws_server.initWSServer(server);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 25 at 5:29

























                answered Mar 25 at 5:24









                JanithJanith

                1,766716




                1,766716





























                    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%2f55331093%2fhow-to-spin-up-a-websocket-server-paralelly-along-with-express-app%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

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                    Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?