Closed gwendall closed 8 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);
});
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 !
If I add it, should it be on another Library like Rooms or should it be integrated in the core library?
I think with the core.
@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?
Sounds good to me ;)
Thanks to @nlhuykhang !
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?