@@ -5,12 +5,68 @@ if (Meteor.isClient) {
5
5
}
6
6
} ) ;
7
7
8
- Meteor . subscribe ( 'allItems' ) ;
8
+ Meteor . subscribe ( 'one' ) ;
9
+ Meteor . subscribe ( 'two' ) ;
9
10
}
10
11
11
12
if ( Meteor . isServer ) {
13
+ var Future = Npm . require ( 'fibers/future' ) ;
14
+
15
+ Meteor . publish ( 'one' , function ( ) {
16
+ var future = new Future ;
17
+
18
+ // send an "added" ddp message
19
+ this . added ( 'items' , Random . id ( ) , { title : 'one' } ) ;
20
+
21
+ // send a "ready" ddp message
22
+ this . ready ( ) ;
23
+
24
+ setTimeout ( function ( ) {
25
+ future . return ( ) ;
26
+ } , 5000 ) ;
27
+
28
+ // Careful! This will block. Try not to block publish functions by yielding
29
+ // the fiber. For example, don't make external API calls. You can, however,
30
+ // put that code in a setTimeout to make it run outside this current fiber.
31
+ // That will result in not blocking downstream publish functions from
32
+ // running.
33
+ return future . wait ( ) ;
34
+ } ) ;
35
+
36
+ Meteor . publish ( 'two' , function ( ) {
37
+ this . added ( 'items' , Random . id ( ) , { title : 'two' } ) ;
38
+ this . ready ( ) ;
39
+ } ) ;
40
+
41
+
12
42
Meteor . publish ( 'allItems' , function ( ) {
13
- return Items . find ( { } , { sort : { order : 1 } } ) ;
43
+ var subscription = this ;
44
+ var cursor = Items . find ( { parentId : 4 } , { sort : { order : 1 } } ) ;
45
+
46
+ // observeChanges is what hooks DDP up to the Mongo database for real time
47
+ // updates. Every time a document that affects this cursor is added, changed
48
+ // or removed, the corresponding callbacks here will be called. Then
49
+ // the appropriate ddp messages are published to the client. The DDP server
50
+ // takes care of making sure a client doesn't get duplicate messages.
51
+ var handle = cursor . observeChanges ( {
52
+ added : function ( id , doc ) {
53
+ subscription . added ( 'items' , id , doc ) ;
54
+ } ,
55
+
56
+ changed : function ( id , doc ) {
57
+ subscription . changed ( 'items' , id , doc ) ;
58
+ } ,
59
+
60
+ removed : function ( id ) {
61
+ subscription . removed ( id ) ;
62
+ }
63
+ } ) ;
64
+
65
+ subscription . ready ( ) ;
66
+
67
+ subscription . onStop ( function ( ) {
68
+ handle . stop ( ) ;
69
+ } ) ;
14
70
} ) ;
15
71
16
72
Meteor . publish ( 'item' , function ( id ) {
0 commit comments