arunoda / meteor-streams

Realtime messaging for Meteor
http://arunoda.github.io/meteor-streams
MIT License
287 stars 97 forks source link

Making use of Meteor Streams as Message Threads #13

Open liveitchina opened 10 years ago

liveitchina commented 10 years ago

Having an issue integrating Meteor-Streams to the application i am creating. The design i am doing is much more similar to Streaming Private Page communication pattern. What will happen is that i have 2 text area with TO and the Message. In the TO field it is the recipient of the message and the MESSAGE field is the main message.

Issue: Meteor-Streams should only send the message to the designated recipient on the TO FIELD, NOT ALL USERS THAT IS ACTIVE. Is there a way on how will i approach this?

arunoda commented 10 years ago

How about this solution.

stream = new Meteor.Stream('private');

if(Meteor.isClient) {
  stream.on(Meteor.userId(), function(message) {
    alert('new message:', message);
  });

  sendMessage = function(message, to) {
    //to is an array of userIds (in your to field)
    stream.emit('send', message, to);
  }
}

if(Meteor.isServer) {
  stream.permissions.write(function(eventName) {
    return eventName == 'send';
  });

  stream.permissions.read(function(eventName) {
    return this.userId == eventName;
  });

  stream.on('send', function(message, to) {
    to.forEach(function(userId) {
      stream.emit(to, message);
    });
  });
}
liveitchina commented 10 years ago

Hi Arunoda,

Thanks for the reply. I will try this out..

ggmacasaet commented 10 years ago

Hi Arunoda,

Does the solution you provided has already a collection attached to it? or that is only reachable if the Receiving User is online?

arunoda commented 10 years ago

I simply use Meteor.userId() for the demonstration purpose. You can use a nickname, random string or anything depending on your app.

ggmacasaet commented 10 years ago

Hi Arunoda,

Sorry for the confusion. What i meant is that are the messages already STORED inside a collection so that whenever a user has logged in he/she can already see the message that is intended for them?

arunoda commented 10 years ago

Okay. Now I got it.

Meteor Streams does not handle the message persistence. You can use stream filters http://arunoda.github.io/meteor-streams/filters.html to store messages to the db. So, in the very first you can get messages via a meteor method or using streams itself.

Is this answer your question?

On Mon, Oct 21, 2013 at 4:39 PM, ggmacasaet notifications@github.comwrote:

Hi Arunoda,

Sorry for the confusion. What i meant is that are the messages already STORED inside a collection so that whenever a user has logged in he/she can already see the message that is intended for them?

— Reply to this email directly or view it on GitHubhttps://github.com/arunoda/meteor-streams/issues/13#issuecomment-26708570 .

Arunoda Susiripala

@arunoda http://twitter.com/arunoda http://gplus.to/arunodahttps://github.com/arunoda http://www.linkedin.com/in/arunoda

iwoork commented 10 years ago

:+1: