Wednesday 25 November 2015

Directory structure of Node project & where to put socket listeners/actions on server side

New to Node. Started a new project with npm using express, jade and added socket.ioNPM created a directory structure as such:/project|- bin|----www <- this is the main start file with server in it|- node_modules|- public|- routes|---- index.js|- views|---- index.jadeapp.js <-- routes to /routes dir filesNow I've more or less figures out the flow and where what goes. However, in including socket.io, I'm not fully appreciating where a few things should go conceptually, nor how data gets passed around with require()So www instantiates the server, requires app.js, which requires the files in /routes (index.js) which routes to the appropriate jade file.So two questions: 1) Where logically should I place my socket.io server-side listeners? Assuming there will be a io.on('connection'..., this should probably go in the www (server) file, but where should my io.on('whatever message'... listeners go? It would make sense that I put them where they'll logically be listening to the sockets from the respective routed files (either app.js or index.js?). Which leads into my second question: 2) To instantiate socket.io, I need to use this code: var io = require('socket.io').listen(server); Where server is now defined in my www file. Suppose I want to define this io in another file, say app.js, how do I pass the 'server' variable up to app.js which is imported with require() into www?Thanks.

Submitted November 26, 2015 at 04:11AM by oldmanandthebanker

No comments:

Post a Comment