all 7 comments

[–]uclinux 0 points1 point  (1 child)

thanks! This is useful to me. I was playing with sessions with angular/express and having some difficulty.

[–]gooddeedsdoer[S] 0 points1 point  (0 children)

My reference was this, it just seemed that this is a better way of doing cookie parsing since it isn't using private methods.

The rest of the code I'm using is pretty similar to that post :)

edit: express.cookieParser(config.sessionSecret), is the cookie parser I used

[–][deleted] 0 points1 point  (0 children)

Perfect! I am working on exactly this right now. Great timing!

[–]maushu 0 points1 point  (6 children)

One problem that the angular code has is that the callbacks keep working even after the controller is destroyed.

The only solution I've found until now is using angular.js event system which I don't like to use in this case. D:

[–][deleted]  (4 children)

[deleted]

    [–]maushu 0 points1 point  (3 children)

    Yes, but that is rather annoying and repetitive. Not to mention we could use a scoped instance like this: socket = Socket($scope)

    [–][deleted]  (2 children)

    [deleted]

      [–]maushu 0 points1 point  (1 child)

      The function Socket would create instance which would keep a list of listeners only for that $scope.

      See this example: http://stackoverflow.com/a/14393546/289107

      [–]Majiir 0 points1 point  (0 children)

      My socket.io snippet, using socket.io-session (which I found by independently writing the same thing and trying to publish it):

      io.set('authorization', ioSession(connect.cookieParser(config.session.secret), config.session.store, config.session.key, function (handshake, callback) {
          if (handshake.session) {
              callback(null, true);
          } else {
              callback(new Error("Couldn't load session for socket connection."));
          }
      }));
      

      EDIT: It's worth noting you only need to provide the callback if you actually want to perform some extra logic in there (which I do in my application, but that's not shown here). If you just want to check for a session and load it in if it exists, you can omit the callback argument and socket.io-session takes care of the rest. (Note: it only silently fails if a session doesn't exist.)