Rebolon
Rebolon

Reputation: 1307

Meteor Streams : client doesn't receive streams

i'm working on a simple app based on meteor and MeteorStreams. The aim is simple : one user will click on a button to create a room other users will join the room those users can emit streams with simple message the creator will listen to that message and then display them

In fact : message from other users are sent (log in server script), but the creator doesn't receive them. If i reload the page of the creator, then it will get messages sent from other user.

I don't really understand why it doesn't work the first time.

I use meteor-router for my routing system. Code can be seen here

https://github.com/Rebolon/MeetingTimeCost/tree/feature/pokerVoteProtection

for the client side code is availabel in client/views/poker/* and client/helpers for the server stream's code is in server/pokerStreams.js

Application can be tested here : http://meetingtimecost.meteor.com The creator must be logged.

If you have any idea, any help is welcome. Thanks

Upvotes: 1

Views: 457

Answers (1)

Rebolon
Rebolon

Reputation: 1307

Ok, Ok, after doing some debugging, i now understand what is wrong in my code : it's easy in fact. The problem comes from the fact that i forgot to bind the Stream.on event to Deps.autorun. The result is that this part of code was not managed by reactivity so it was never re-run automatically when the Session changed.

The solution is so easy with Meteor : just wrap this part of code inside the Deps.autorun

Meteor.startup(function () {
  Deps.autorun(function funcReloadStreamListeningOnNewRoom () {
    PokerStream.on(Session.get('currentRoom') + ':currentRoom:vote', function (vote) {
      var voteFound = 0;
      // update is now allowed
      if (Session.get('pokerVoteStatus') === 'voting') {
        voteFound = Vote.find({subscriptionId: this.subscriptionId});
        if (!voteFound.count()) {
          Vote.insert({value: vote, userId: this.userId, subscriptionId: this.subscriptionId});
        } else {
          Vote.update({_id: voteFound._id}, {$set: {value: vote}});
        }
      }
    });
  });
});

So it was not a Meteor Streams problem, but only my fault. Hope it will help people to understand that outside Template and Collection, you need to wrap your code inside Deps if you want reactivity.

Upvotes: 2

Related Questions