How do I use subscription in a streaming router?

// server/publications.js
Meteor.publish('crewMessages', function(crewId) { 
  return CrewMessages.find({crewId: crewId}, {sort: {submitted: -1}, limit: 100});
});

// lib/router.js
FlowRouter.route('/crew/:crewSlug', {
    subscriptions: function(params) {
        console.log("Subscribed to this crew chat messages:", params.crewSlug);
        this.register('crewMessages', Meteor.subscribe('crewMessages', params.crewSlug));
    },
    action: function(params) {
      FlowLayout.render("layout", { content: 'crew' });
    }
});

      

And inside my template crew.html

:

<div class="container">
  {{> crewChat}}
</div>

      

And mine crewChat.html/js

:

Template.crewChat.helpers({
  messages: function() {
    return CrewMessages.find({}, {sort: {submitted: -1}}); 
  }
});

<div class="ui feed">
  {{#each messages}}
    {{> crewChatMessage}}
  {{/each}}
</div>

      

In my crewChat.js

file, how can I use the subscription set in Flow-router?

+3


source to share


1 answer


I am creating a functional example for you in MeteorPad

http://meteorpad.com/pad/Ba5DTe94NjFi3ZTPA/Playground_Flow-Router_Chat

The most important thing is that you can use the collection inside your template with help Collection.find()

, because you have only subscribed to messages from the crews by route subscription.

Each message can be received as a regular document see template showChatMessage



Hope this is clear to you.

Cheers Tom

PS: You can also change the url inside the meteogram to switch to chats for team2 and team3

+3


source







All Articles