GithubHelp home page GithubHelp logo

Comments (9)

inadarei avatar inadarei commented on May 18, 2024

Thank you for your message, Carlos.

Couple questions:

  1. Do you need to use HTTP and Sockets or just sockets?
  2. Can you please send a PR with your changes so I can see exactly what you are trying to do. The intent is not to merge but better understand what flexibility you need.

I think the clean way here is to restructure things so that such extensions are possible in a modular way and I am very open to doing it.

Thanks

from nodebootstrap.

carlosbaraza avatar carlosbaraza commented on May 18, 2024

Hi @inadarei,

Thank you for your quick answer. I still did not find the best way to really modularise the application, and this is the point where I am stuck. These are the modifications I did on the nodebootstrap-server/app.js:

...
  , http    = require('http');
...
  if (is_http_thread) {
    http = http.createServer(app);
    http.listen(CONF.app.port);
  }
...
...
  callback(app, http);
...

Then, in the server.js file I used the http handler to bind socket.io:

  var io = require('socket.io')(http);

  // Use example directly from the server.js file. It should be used in the modules.
  io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
      console.log('a user disconnected')
    })
  });

However, now the problem that I am facing is that I would like to use my socket.io io object on the individual modules without braking the no-global-variables rule. At this point I have these doubts:

  1. I am not really sure if I should find a way to bind socket.io in the module itself. Maybe finding the http server and creating a new http instance? However, it feels awkward.
  2. I guess I am facing this issue because I did not actually understand how modules can require('express') and create a new app that would be exported to the main app using the module. This approach, tough it is really nice to modularise the app, makes it impossible to use any configuration from the main app in the modules.

How would you get socket.io to work in the modules?

Thank you,
Carlos.

from nodebootstrap.

inadarei avatar inadarei commented on May 18, 2024

Thank you Carlos.

Work on making NodeBootstrap to be more modular is well on its way and I hope to be able to support your use case soon.

Appreciate all your information.

from nodebootstrap.

one-smart avatar one-smart commented on May 18, 2024

+1

from nodebootstrap.

feynmanliang avatar feynmanliang commented on May 18, 2024

+1

from nodebootstrap.

inadarei avatar inadarei commented on May 18, 2024

@carlosbaraza @feynmanliang

how does something like the following look: https://github.com/inadarei/nodebootstrap/blob/master/server.js#L13

Since websockets are not URL-path-driven and are event-driven, we don't actually need to do as much "magic" as we had with the callback support for HTTP routes. Each module just needs access to socketio instance and it can be passed-through during module initialization. Multiple modules can/should share the same instance as in the event-driven workflow, multiple modules are "allowed" to respond to the same event.

All you should need is: pass websockets instance to the module, during inclusion.

Sounds OK?

from nodebootstrap.

feynmanliang avatar feynmanliang commented on May 18, 2024

Exactly what I have been doing. However, I think you may have meant to write runningApp.http (https://github.com/inadarei/nodebootstrap-server/blob/master/app.js#L40)?

On Mon, Apr 13, 2015, 00:03 Irakli Nadareishvili [email protected]
wrote:

@carlosbaraza https://github.com/carlosbaraza @feynmanliang
https://github.com/feynmanliang

how does something like the following look:
https://github.com/inadarei/nodebootstrap/blob/master/server.js#L13

Since websockets are not URL-path-driven and are event-driven, we don't
actually need to do as much "magic" as we had with the callback support for
HTTP routes. Each module just needs access to socketio instance and it can
be passed-through during module initialization. Multiple modules can/should
share the same instance as in the event-driven workflow, multiple modules
are "allowed" to respond to the same event.

All you should need is: pass websockets instance to the module, during
inclusion.

Sounds OK?


Reply to this email directly or view it on GitHub
#26 (comment)
.

from nodebootstrap.

carlosbaraza avatar carlosbaraza commented on May 18, 2024

Hi @inadarei,

This is the way I was doing too, passing the io object around.

I am glad this idea made all its way to master :D.

from nodebootstrap.

inadarei avatar inadarei commented on May 18, 2024

Awesome! Sounds great. Glad that websockets it nicely supported now. Thanks, guys!

@feynmanliang, yes the code sample was supposed to have runningApp.http. Good catch! I updated the file, now. Thank you.

from nodebootstrap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.