Skip to content

Instantly share code, notes, and snippets.

@cmather
Created August 18, 2015 23:45
Show Gist options
  • Save cmather/bbdc893bc2eb9e21451f to your computer and use it in GitHub Desktop.
Save cmather/bbdc893bc2eb9e21451f to your computer and use it in GitHub Desktop.
Rooms = new Mongo.Collection('rooms');
Messages = new Mongo.Collection('messages');
if (Meteor.isClient) {
Meteor.subscribe('rooms', function () {
var room = Rooms.findOne();
Meteor.subscribe('messages', room._id);
});
}
if (Meteor.isServer) {
Meteor.publish('rooms', function () {
return Rooms.find();
});
Meteor.publish('messages', function (roomId) {
var cursor = Rooms.find({_id: roomId});
var self = this;
var publishMessages = function (roomId) {
Messages.find({roomId: roomId})._publishCursor(self);
};
cursor.observeChanges({
added: function (roomId, room) {
if (room.isAccepted)
publishMessages(roomId);
},
changed: function (roomId, room) {
if (room.isAccepted) {
publishMessages(roomId);
}
}
});
this.ready();
});
if (Rooms.find().count() == 0) {
var roomId = Rooms.insert({name: 'My Room', isAccepted: false});
Messages.remove({});
Messages.insert({roomId: roomId, message: 'Hello'});
Messages.insert({roomId: roomId, message: 'World'});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment