YuukanOO / streamy

Use meteor underlying sockets for realtime communications
https://atmospherejs.com/yuukan/streamy
MIT License
105 stars 20 forks source link

Easiest way to get the emitter's userId in .on() #17

Closed gwendall closed 8 years ago

gwendall commented 9 years ago

The .on() callback on the client-side passes the sender's session ID as I see in the doc. In my case I would like to get the sender's userId. Is there an easy built-in way to get that?

YuukanOO commented 9 years ago

Nope but you can do it yourself when emitting. Something like this:

// When emitting
Streamy.emit('your-message', { _uid: Meteor.userId(), other_data: 'Other value' });

// When receiving
Streamy.on('your-message', function(data) {
  console.log(data._uid, data.other_data);
});
gwendall commented 9 years ago

This is not ideal as anyone could basically "fake" being this user by sending this parameter. I ended up doing like that.

  Meteor.startup(()=>{
    Tracker.autorun((tracker)=>{
      if (Meteor.userId() && Streamy.id()) {
        let selector = { _id: Meteor.userId() };
        let modifier = { $set: {'profile.socketSession': Streamy.id() }};
        Meteor.users.update(selector, modifier);
        tracker.stop();
      }
    });
  });
  let getUserId = (sessionId)=>{
    let user = Meteor.users.findOne({ 'profile.socketSession': sessionId });
    return user && user._id;
  };
  Streamy.on('your-message', (data)=>{
    let fromUser = getUserId(data.__from);
    // etc.
  });

I insist there should be a built-in way to know which user streams come from. Great library anyways, thanks !

YuukanOO commented 9 years ago

If I add it, should it be on another Library like Rooms or should it be integrated in the core library?

Yayap13 commented 8 years ago

I think with the core.

nlhuykhang commented 8 years ago

@YuukanOO I think I can help you with this enhancement. My only concern is how the api should look like. from already points to sid, modifying it would be a bad idea. How about a new field named fromUserId?

YuukanOO commented 8 years ago

Sounds good to me ;)

YuukanOO commented 8 years ago

Thanks to @nlhuykhang !