How do you store data server side that is specific to a client in Meteor?

后端 未结 6 818
陌清茗
陌清茗 2021-02-13 02:15

Express implements a server side session object that lets you store data specific to a client. How would you do the equivalent in Meteor?

strack recommended using a coll

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 03:11

    I think a "meteor" way to do this is:

    On server side create and publish a ClientSession collection

    UserSession = new Meteor.Collection("user_sessions");
    
    Meteor.publish('user_sessions', function (user) {
    
        return UserSession.find(user);    
    });
    

    On client side

    Session.set('user_id', 42);
    
    UserSession = new Meteor.Collection("user_sessions");
    Meteor.subscribe('user_sessions', Session.get('user_id'));
    

    You now have an application-level UserSession object that is specific to that user that you can put/get stuff.

    Also, you can manipulate the UserSession collection on the server using Meteor#methods.

提交回复
热议问题