Tuesday 19 January 2016

SocketIO: Suggestions on clean way to add events to every client?

Hi there, I have a basic chat/video app that connects the user through WebRTC. Right now I handle the majority of the socketio code through the main app.js file, I'd like to pull the files out to make them testable and improve readability.I was thinking about doing something likelet MyClient = require('myclient') io.on('connection', function(socket){ MyClient(socket.client); Then inside 'myclient' I'd have an object of functions mapped to their event names.var events = { 'join':function(), 'leave':function(), } And one function that would do a for in loopfor(key in events){ client.on(key, events[key]); Is there a better way to do this, or a standard way?

Submitted January 19, 2016 at 11:24PM by fallen77

No comments:

Post a Comment