GithubHelp home page GithubHelp logo

Comments (2)

jondubois avatar jondubois commented on May 11, 2024

Not sure if that's any use but anyway: Regarding your question on mongo's req.user-object object:
You can access the socket's HTTP handshake request object using socket.request... So if you're using express with HTTP middleware which adds the user-object, in theory you should be able to access it using socket.request['user-object']. I haven't tested this, but I'm confident it would work.

Nonetheless, I think using the default SC session store may be easier because it handles distribution across multiple store processes for you and it integrates nicely with SC.

You don't need to add any middleware to express in order to use SC's session store - It's there by default. You can access it through either the socket or HTTP req object (socket.session or req.session).

The session object is effectively an nData client (https://github.com/TopCloud/ndata) which is scoped to a particular session ID. You can use all he methods exposed by the nData client (except the ones related to pub/sub - See link above for API).

So in your example where you use passport.authenticate(), if you have a reference to either a socket object or a HTTP req object, you can just use something like:

req.session.set('user', req.user, function (err) {
  // This is an optional callback to check if data was set successfully on local nData store process
});

Then later, if you need to get the data out of this session:

req.session.get('user', function (err, data) {
  // ... If data is undefined then you know that the request's current session was never authenticated
});

Hopefully that answers your questions. Don't hesitate to ask more questions if you run into any issue. Also suggestions are welcome.

If anyone is interested; The clustering engine (which provides the session object) is a modular component of SC (can be swapped out), the default one (and currently the only one) is called IOCluster: https://github.com/TopCloud/iocluster. Ideally, it would be nice to make alternatives with different popular storage engines like Redis and Mongo.

from socketcluster.

mafrost avatar mafrost commented on May 11, 2024

Oh how did I not see that! Thank you so much. Both of the solutions worked fine but as you said, the latter one (ndata) works just as good and I don't need to use mongo for sessions!

Thanks again, fast response and good explanations!

from socketcluster.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.