fnakstad / angular-client-side-auth

One way to implement authentication/authorization in Angular applications
http://angular-client-side-auth.herokuapp.com/
MIT License
1.63k stars 346 forks source link

Looking for a best practice of how to include socket.io as module and used in controllers #72

Closed michaeljoser closed 10 years ago

michaeljoser commented 10 years ago

I would really like some advice on how to integrate socket.io in the setup we got here.

For the client side (i.e. AngularJS) I'm using Brian Ford way to include socket.io into AngularJS (https://github.com/btford/angular-socket-io). That is working fine and I am happy with the result.

But on the server side i've put all the socket.io related code in server.js which is surely a really bad way of doing things. What would be nice is to have the socket code in a module and in the controllers i could require it and be able to use the io and socket variable... not really sure how to implement it nicely though.

Looking for a "proper" way to implement socket.io in a module so that it can be used in the controllers or routes...

Any help most welcomed!

fnakstad commented 10 years ago

Hi there! I would recommend you think of the different event names you have as traditional routes. Then you can have something similar to a routing file where you define all the event names of your app, and delegate the actual callbacks to functions implemented in controllers. What do you think about such an approach?

michaeljoser commented 10 years ago

Yes indeed that's exactly the road i followed and quite happy with it actually.

Some details for those interested:

// file: server.js

// at the end i added
require('./server/controllers/socket.js')(server);
// file: ./server/controllers/socket.js

module.exports = function(server) {
    var io = require('socket.io').listen(server);

    io.sockets.on('connection', function (socket) {

        socket.on('event', function (data) {
            console.log(data);
            socket.emit('reply-event', { msg: 'reply to event'} );
        });

        require('../controllers/shoutbox.js')(io, socket);
        require('../controllers/others_here.js')(io, socket);
    });
}
// file: ./server/controllers/shoutbox.js

var Shoutbox = require('../models/Shoutbox.js');

module.exports = function(io, socket) {

    socket.on('shoutbox:addShout', function (shout) {
         // do stuff here and use the model
         Shoutbox.add(shout);
    }

    // etc etc

}
// file: ./server/models/Shoutbox.js

// db connect code here
module.exports = {
      getAll: function () {
          // retrieve from db
      },

      add: function (shout) {
         // logic here to add to db
     }

}

Took me quite a while to figure out that structure but im quite happy with it...

Hope it helps anyone :)

Thanks

fnakstad commented 10 years ago

Glad to hear it worked out for you, and thanks so much for sharing your solution!

nishmeht7 commented 5 years ago

@michaeljoser your implementation is great. But I've got a question, how do you trigger a socket event in the server from some other file? For example, if you wanted to emit the socket in controllers/shoutbox.js after certain tasks were completed, how would you call this function without having an instance to the server?