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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript