Organizing Socket IO code

1,671 views
Skip to first unread message

Mike Chen

unread,
Jun 30, 2013, 12:50:29 AM6/30/13
to nod...@googlegroups.com
Hi guys,

I'm pretty new to Node.JS and socket IO, so I was wondering if I could get some guidance on project I'm working on. I would like to build a single page multiplayer game that basically communicates solely through socket.IO. 

However, I am quickly seeing that my socket.js file is getting very clumsy. I am using express to manage the MVC side of things, but I feel like socket connections don't really fit in the MVC paradigm. 

I have read a bunch of tutorials on node JS and socket IO, but all of them have small socket components. 

Essentially I am wondering how to break down a large socket.js file into smaller more manageable components? For example, I have a chat room in the page, as well as the game part, and some meta game aspects. I don't feel like these all belong in the same socket.js file, but it doesnt seem clean to divide them in separate files either because you need access to the client and the socket object ( to emit to all users in a room lets say) 

Thanks



Duncan Wilkie

unread,
Jun 30, 2013, 6:38:00 AM6/30/13
to nod...@googlegroups.com
Pass the socket.io variable around as a parameter to other files / components
var io = socket.io

require('./component1.js')(io)

...
// component1.js 
​module.exports = function (io) {
// logic

Sent from Mailbox for iPhone


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Antoine van Wel

unread,
Jun 30, 2013, 8:51:21 AM6/30/13
to nod...@googlegroups.com
Create separate "service" modules which are completely unaware of socket.io, which accept hooks/callbacks; then in your socket.js wire them together. 

For example for your chat, create a module which does the room creation / chatting / joining etc and let it do callbacks whenever a room is created / somebody says something / etc. This should keep your socket.js file down to a minimum and at the same time lead to more flexible, reusable code.

Then if your socket.js file is still too long you can further break it down as suggested by Duncan, but personally I would keep the wiring in a single file.



Antoine




Eric Mill

unread,
Jun 30, 2013, 4:40:38 PM6/30/13
to nod...@googlegroups.com
Antoine's solution is the right direction. Keep all of your "routes" (endpoints) in one file, and interact with the various modules from there. This will keep your code much more flexible without adding much complexity, and will leave you more free to experiment with things, and even swap out socket.io later on if you so chose (sockjs is a pretty terrific alternative, for example).

Ken

unread,
Jul 1, 2013, 11:39:18 AM7/1/13
to nod...@googlegroups.com
If express is feeling like overkill but you'd still like some framework support, I suggest trying restify (http://mcavage.github.io/node-restify/#Socket.IO). I find myself turning to it as a starting point for many projects these days.

Filipe Deschamps

unread,
Jul 8, 2013, 7:15:27 AM7/8/13
to nod...@googlegroups.com
Antoine, this seems very interesting!

Do you have any example code?

Mike Chen

unread,
Jul 8, 2013, 11:25:03 PM7/8/13
to nod...@googlegroups.com
Cool, Thanks Antoine. I'll give it a shot. If you have any pointers / example code, let me know!

Thanks,
-Mike 

Alex Kocharin

unread,
Jul 13, 2013, 2:23:13 AM7/13/13
to nod...@googlegroups.com

You can pass client/socket object and whatever else you need to different files. I see no problem with that. Also, socket.io supports namespaces exactly for this kind of things.

PS: another good solution would be to forget about MVC forever ;)
Reply all
Reply to author
Forward
0 new messages