Reactivity and Triggering an event on the other end - so to speak

I’ve been through the tutorial, and it was excellent. In the app I’m building, I’m trying to understand, how on 1 client I can perform some action (or the user can), and this action can ‘message’ out to other subscribers and that message can be used to automatically generate a reaction from other clients.

Think along the lines of an unattended application.

You send a message, and you get the ‘Delivered’ message. I don’t want this to only mean it made it to the server, but it actually made it to the other client(s).

I’ve been using socket.io in Nodejs, and undeerstadn how to message back and forth on it, so if there is a parallel, that would help me tremendously. Maybe I’m just making this harder than it has to be.

Any help is appreciated.

Save the message to the database (you probably want to do this anyway) and have other clients subscribe to the messages collection. Meteor will take care of the rest.

Just want oto 1. make sure I’m being clear with what I want to do, and 2. make sure I’m understanding.

I want to send out a ‘message’ from client 1. I want subscribed clients to receive the message, and automatically send back some sort of message to client 1 that the message has been received.

From your response @manuel, I’m understanding that Meteor will do this if I just save the original message to the database.

What signal / notification is received on the other end when a message is sent. How can I taylor the return message back to the originator?

If there are more tutorials out there on this, please let me know.

Perhaps I need to see about using the this.changed() event. Maybe I can watch the collection for a change to a document, and trigger by that change…maybe.

This should point you in the right direction. You still need to clean it up a bit, for example add allow rules and set the created time on the server using UTC.

Create a new project, remove autopublish, and add manuel:viewmodel (not necessary for what you’re doing but it makes it easier for me to create the demo)

client/body.html

<body>
  New Message: <input {{b "value: newMessage"}}>
  <button {{b "click: sendMessage"}}>Send Message</button>
  <h3>Last 10 messages:</h3>
  <ul>
    {{#each messages}}
      <li>{{message}}</li>
    {{/each}}
  </ul>
</body>

client/body.js

Template.body.viewmodel({
  sendMessage() {
    Messages.insert({ message: this.newMessage(), createdAt: new Date() });
    this.newMessage('');
  },
  messages() {
    return Messages.find({}, { sort: { createdAt: -1 }, limit: 10 });
  },
  onCreated() {
    Meteor.subscribe('messages', new Date());
    Messages.find().observe({
      added(doc) {
        console.log( doc.message );
      }
    });
  }
});

collections/messages.js

Messages = new Mongo.Collection('messages');

server/messages.js

Meteor.publish('messages', function(startDate) {
  return Messages.find({ createdAt: { $gt: startDate }}, { sort: { createdAt: -1 }, limit: 10 });
});