[Question] How would be the best way to integrate Socket.io?
carlosbaraza opened this issue · comments
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.
Thank you for your message, Carlos.
Couple questions:
- Do you need to use HTTP and Sockets or just sockets?
- 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
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:
- 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. - I guess I am facing this issue because I did not actually understand how modules can
require('express')
and create a newapp
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.
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.
+1
+1
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?
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/feynmanlianghow does something like the following look:
https://github.com/inadarei/nodebootstrap/blob/master/server.js#L13Since 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)
.
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.
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.