Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How would be the best way to integrate Socket.io? #26

Closed
carlosbaraza opened this issue Feb 22, 2015 · 9 comments
Closed

[Question] How would be the best way to integrate Socket.io? #26

carlosbaraza opened this issue Feb 22, 2015 · 9 comments

Comments

@carlosbaraza
Copy link

As part of my project, I need to integrate socket.io.

The way I found the cleanest was to modify the nodebootstrap-server to use the http library when listening the port and exposing the http handle to the callback so I could use that http handler in the server.js file to bind socket.io.

Any ideas on how to do this better? To be honest I don't like to depend on the nodebootstrap-server module. Maybe it could be a better aproach to expose those files in the project folder so they could be modified when needed (like my case)?

Thank you for this great bootstrap project.
Carlos.

@inadarei
Copy link
Owner

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

@carlosbaraza
Copy link
Author

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.

@inadarei
Copy link
Owner

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.

@one-smart
Copy link

+1

1 similar comment
@feynmanliang
Copy link

+1

@inadarei
Copy link
Owner

@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?

@feynmanliang
Copy link

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 notifications@github.com
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)
.

@carlosbaraza
Copy link
Author

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.

@inadarei
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants