Code Monkey home page Code Monkey logo

meteor-reactive-publish's Introduction

reactive-publish

This Meteor smart package extends publish endpoints with support for reactivity so that you can use server-side autorun inside a publish function.

After adding this package you can use server-side Tracker.autorun inside your publish function and any published documents will be automatically correctly send to the client while your reactive computation will rerun when any dependency triggers invalidation. Only changes to documents between reruns will be send to the client. As a rule, things work exactly as you would expect, just reactively if they are inside an autorun. You can use any source of reactivity, like reactive server-side MongoDB queries and reactive variables.

Publish function's this is extended with this.autorun which behaves the same as Tracker.autorun, but you can return a cursor of array of cursors you want to publish, and this inside the computation function is bound to the publish context. Moreover, computation is automatically stopped when subscription is stopped. If you use Tracker.autorun you have to take care of this yourselves, or you can return a computation from the publish function to have it stopped automatically as well.

Server side only.

Installation

meteor add peerlibrary:reactive-publish

Additional packages

  • peerlibrary:subscription-data – Support for reactive and shared subscription data context which allows you to change arguments to the publish function without restarting it, and have a side channel to communicate metadata back to the subscriber as well

Examples

You can make a simple publish across an one-to-many relation:

Meteor.publish('subscribed-posts', function () {
  this.autorun(function (computation) {
    var user = User.findOne(this.userId, {fields: {subscribedPosts: 1}});
    
    return Posts.find({_id: {$in: user && user.subscribedPosts || []}});
  });
});

You can make queries which are based on time:

var currentTime = new ReactiveVar(Date.now());

Meteor.setInterval(function () {
  currentTime.set(Date.now());
}, 1000); // ms

Meteor.publish('recent-posts', function () {
  this.autorun(function (computation) {
    return Posts.find({
      timestamp: {
        $exists: true,
        $gte: currentTime.get() - (60 * 1000) // ms
      }
    }, {
      sort: {
        timestamp: 1
      }
    });
  });
});

You can make complicated but reactive permission checks. For example, support user groups:

Meteor.publish('posts', function () {
  this.autorun(function (computation) {
    var user = User.findOne(this.userId, {fields: {groups: 1}});
    
    return Posts.find({
      $or: [{
        'access.userId': user && user._id
      }, {
        'access.groupId': {
          $in: user && user.groups || []
        }
      }]
    });
  });
});

Discussion

Adding this package to your Meteor application will make all MongoDB queries reactive by default (you can still specify reactive: false to queries to disable reactivity for a specific query, or use Tracker.nonreactive). It will also automatically enable server-side autorun. All this might break some existing server-side code which might not expect to be reactive. Inspect locations where your code or packages you are using already (before using this package) call Tracker.autorun on the server. In most cases this occurs only in the code which is shared between client and server.

While documents are send to the client only once and in later reruns of computations only changes are send, the server side still has to make a new query and compute a diff what to send for every rerun, so this approach is suitable for reactivity which is not common, but you still want to support it. For example, queries with reactive permission checks often will not change during the life-time of a query because permissions change rarely. But if they do, a user will see results reactively and immediately.

Consider also optimizing your autoruns by splitting them into multiple autoruns or by nesting them. You can also use computed fields to minimize propagation of reactive change.

When using this approach to support reactive joins it is most suitable for one-to-many relations, where the "one" document changes infrequently. For many-to-many joins consider publishing collections separately and join them on the client side. The issue is that for any change for the first "many" documents, the computation will be invalidated and rerun to query for second set of "many" documents. Alternatively, you can consider using PeerDB which effectively denormalizes joins across many-to-many relations and allows direct querying and publishing.

Feel free to make pull requests with optimizations.

Note that calling onStop inside a reactive computation probably does not do what you want. It will register a callback to be called when the whole publication is stopped and if you are doing this inside an autorun this means that a new callback is registered every time the computation is rerun. Which can potentially add many callbacks. Moreover, they will be called only after the whole publication stops, and not between computation reruns. What you probably want is to use Tracker.onInvalidate which runs when the computation itself is invalidated or stopped. Do notice that for reactive queries and observes inside a computation it is not needed to free their resources by yourself because that will be done already automatically.

Acknowledgments

This package is based on the great work by Diggory Blake who made the first implementation.

Related projects

There are few other similar projects trying to address similar features, but theirs APIs are cumbersome and are different than what developers are used to for Meteor.

  • meteor-publish-with-relations – complicated custom API not allowing to reuse existing publish functions, which means no support for custom publish with added/changed/removed, no support for other reactive sources
  • meteor-smart-publish – complicated custom way of defining dependencies and works only with query cursors and not other reactive sources
  • reywood:publish-composite – allow you to define a nested structure of cursors, which get documents from higher levels in a reactive manner, but it works only with only with query cursors and not custom added/changed/removed functions or other reactive sources
  • copleyjk:simple-publish – seems similar to meteor-publish-with-relations, but a more developed version covering more edge cases; on the other hand it has the same limitations of no support for added/changed/removed or other reactive sources
  • peerlibrary:related – our previous implementation with different API and no reactivity support, but with support for custom added/changed/removed publishing

meteor-reactive-publish's People

Contributors

etyp avatar mitar avatar wildhart avatar zodern avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meteor-reactive-publish's Issues

Getting Fiber/bindEnvironment error in Meteor 1.3 beta 7

In using peerlibrary:reactive-publish with Meteor 1.3-modules-beta.7 I get the following error when a user requests a webpage:

E20160205-10:55:38.281(-8) (webapp_server.js:716) Error running template: Error: Meteor
code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor
libraries with Meteor.bindEnvironment.

This occurs regardless of whether or not this.autorun is used.

reactive-publish & publish-counts error: no method 'getCollectionView' (server-side rendering)

I'm calling Counts.publish() from the publish counts package within a server-side autorun, and it sometimes triggers the following error:

 Exception from Tracker recompute function:
 TypeError: Object [object Object] has no method 'getCollectionView'
     at PublishContext.publish.added (packages/peerlibrary_reactive-publish/packages/peerlibrary_reactive-publish.js:291:1)
     at Object.Counts.publish (packages/tmeasday_publish-counts/server/publish-counts.js:79:1)
     at PublishContext.<anonymous> (packages/nova:posts/lib/server/publications.js:79:12)
     at Computation.<anonymous> (packages/peerlibrary_reactive-publish/packages/peerlibrary_reactive-publish.js:313:1)
     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)
     at packages/meteor/dynamics_nodejs.js:123:1
     at packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:446:1
     at packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:424:1
     at Function.FiberUtils.synchronize (packages/peerlibrary_fiber-utils/packages/peerlibrary_fiber-utils.js:163:1)
     at Computation.Tracker.Computation.Computation._runInside (packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:413:1)

Here's the relevant publication code: https://github.com/TelescopeJS/Telescope/blob/devel/packages/nova-posts/lib/server/publications.js#L71-L91

Multiple cursors for collection

I have a publication with nested autorun

Meteor.publish('Flow.flowsOfOwner', function (owner) {
  this.autorun(function () {
    const ownerRelationsCursor = FlowUserRelations.find({type: 'owner', userId: owner})
    const flowIds = ownerRelationsCursor.map(prop('flowId'))
    this.autorun(function () {
      const flowsCursor = Flows.find({_id: {$in: flowIds}})
      return [ownerRelationsCursor, flowsCursor]
    })
  })
})

It works fine initially, but if I made any change to FlowUserRelations collection, it would crash

 Exception from sub Flow.flowsOfOwner id r3ZgrMWZcQa6eAeGq Error: Multiple cursors for collection 'flow_user_relations'

Subscriptions rerun each 10 seconds

I am seeing a strange issue.

I am actually publishing Meteor.user account data, so that user data is accessible to an admin.

On my development machine, this app is running correctly.

But when I deploy, it reruns subscriptions every 10 seconds. I only call subscribe in a single place, in the default waitOn of IronRouter. It should rerun only if the userId changes. When I login the waiton waits for subscription calls. I then expect it to be done.

But it runs again in 10 seconds, and again, and again - but only when its deployed.

Any ideas?

Also is reactive-publish driven by a 10 second timer?

Thanks

Stopping subscription before it's ready can leave observers running forever

The issue

I've found an edge case when using this package alongside meteorhacks:unblock's publication unblock where stopping a subscription before it's ready on the client will leave reactive finds inside publish autoruns open forever, even after a session is closed.

To reproduce

To reproduce, you'll need a publication handle which uses both this.unblock() and this.autorun() like so:

// Where: Items = Meteor collection
Meteor.publish('items', function () {
  this.unblock();
  this.autorun(() => {
    // Some arbitrary findOne/fetch/map
    OtherCollection.findOne();
    return Items.find({});
  });
});

Then on the client, if you subscribe to the publication but stop it before it's ready like:

const handle = Meteor.subscribe('items');
handle.stop();

The Items cursor will stop as expected, but the OtherCollection cursor will exists forever, even if the user closes out their session. This issue in tandem with the issue where finds on autoruns always use polling driver can lead to rough performance on servers even after users close out their sessions.

Temporary workaround

For now, you can keep track of a publication's computations and stop them inside of a publish.onStop callback. This will ensure that any reactive queries in the autorun are stopped:

Meteor.publish('items', function () {
  this._trackedComputations = [];
  this.onStop(() => {
    while (this._trackedComputations.length) {
      const c = this._trackedComputations.shift();
      c.stop();
    }
  });
  this.unblock();
  this.autorun((computation) => {
     this._trackedComputations.push(computation);
    // Some arbitrary findOne/fetch/map
    OtherCollection.findOne();
    return Items.find({});
  });
});

Memory leak server crash with (0.3.0)

We are using this package in production and we are experiencing huge memory leaks.

When I continuously update the items that are subscribed in this publication then the memory is continuously increasing and server is getting crashed.
Our publication looks like this

Meteor.publish("getTwoLevelTests", function getTwoLevelTests(query) {
	this.autorun(function (computation) {
		let item = Test.findOne({ "_id": query._id }, { fields: { "links": 1 } });
		let itemIds = [];
		if (item && item.links) {
			item.links.forEach((link) => { link.linkedItemId && !itemIds.includes(item.linkedItemId) && itemIds.push(item.linkedItemId) });
		}
		return Test.find({ companyId: query.companyId,"links.linkedItemId": { $in: itemIds } })
	});
});

When I try to upgrade to latest version (0.4.0) of peerlibrary:reactive-publish I am getting this error "ReferenceError: ReactiveVar is not defined"

Can someone help here ?

this.unblock not working (is this intended?)

Hi all,

I noticed that with this library I get an error when using this.unblock() in a publish method:

stack: 'TypeError: Object [object Object] has no method 'unblock'\n at [object Object]. (app/packages/issuebrowser/
server/index.js:138:8)\n at [object Object]. (packages/peerlibrary_reactive-publish/packages/peerlibrary_reactive-publish.js:310:1

Any workaround that one can still use this.unblock? Or is there a problem with allowing it with this plugin?

Best regards

Performance notes?

How does well this package perform compared to publish-composite?
Seems to be pretty heavy on the server.

Question: Meteor 1.8.1 - ReferenceError: ReactiveVar is not defined

I'm seeing this error with Meteor 1.8.1, I do have [email protected] installed with my project.

W20190603-22:49:17.309(-4)? (STDERR) ReferenceError: ReactiveVar is not defined
W20190603-22:49:17.309(-4)? (STDERR)     at _fn (packages/peerlibrary_reactive-publish.js:421:5)
W20190603-22:49:17.309(-4)? (STDERR)     at packages/peerlibrary_reactive-publish.js:1501:3
W20190603-22:49:17.310(-4)? (STDERR)     at packages/peerlibrary_reactive-publish.js:1506:4
W20190603-22:49:17.310(-4)? (STDERR)     at packages/peerlibrary_reactive-publish.js:1512:3
W20190603-22:49:17.310(-4)? (STDERR)     at /Users/user/Projects/project/project/.meteor/local/build/programs/server/boot.js:419:36
W20190603-22:49:17.310(-4)? (STDERR)     at Array.forEach (<anonymous>)
W20190603-22:49:17.310(-4)? (STDERR)     at /Users/user/Projects/project/project/.meteor/local/build/programs/server/boot.js:228:19
W20190603-22:49:17.310(-4)? (STDERR)     at /Users/user/Projects/project/project/.meteor/local/build/programs/server/boot.js:479:5
W20190603-22:49:17.311(-4)? (STDERR)     at Function.run (/Users/user/Projects/project/project/.meteor/local/build/programs/server/profile.js:510:12)
W20190603-22:49:17.311(-4)? (STDERR)     at /Users/user/Projects/project/project/.meteor/local/build/programs/server/boot.js:478:11
=> Exited with code: 1

Any ideas?

why it doesn't work for me??help

server publish:

Meteor.publish("loanTotal", function() {
    let user = Meteor.users.findOne({
        _id: this.userId
    });
    this.autorun(function () {
      console.log("enter in autorun!",user.username);
      //var result = loanLogs.aggregate(pipeline);
      var result = loanLogs.findOne({own:user.username,time:{$gt:new Date("2016-09-01")}});
      todayLoanTotal.remove({});
      todayLoanTotal.insert(result);
      return todayLoanTotal.find();
   });
});

client substrible:
Meteor.subscribe('loanTotal');

Now, when loanLogs changes, it doesn't trigger the autorun function, why? Do I miss something else?

Is reactive-publish expected to work with this.changed on a "virtual" collection?

Environment

Meteor v1.7.0.3
reactive-publish 0.7.0

Repro

I am using reactive-publish to join some data on my server and then publish a "virtual" collection. That is... a collection that does not exist on the server.

I am using this.added, this.changed and this.removed to manage the collection.

Initially, I implemented with only this.added and this.removed. All seemed to work fine, but in an effort to reduce the amount of data I send down the wire, I implemented this.changed.

All my calls and data on my end are working as expected, but a this.removed is being sent by reactive-publish that deletes my document.

Here is my DDP trace:

debug.js:12 receive {msg: "added", collection: "test", id: "sq3fyk6LZ63vBD3oX", fields: {…}}
debug.js:12 receive {msg: "changed", collection: "test", id: "sq3fyk6LZ63vBD3oX", fields: {…}}
debug.js:12 receive {msg: "removed", collection: "test", id: "sq3fyk6LZ63vBD3oX"}

the this.removed was sent by reactive-publish

Result: the document on the client is removed ☹️
Expected: reactive-publish to not send a removed and the document would be changed.

Observations

reactive-publish is inserting a this.removed because it believes that there are no added documents at the code here. I think the reason it believes this is because of this line.

(I am unclear on why the documents object would be reset)

Issue when using idGeneration: 'MONGO'

While using this module I sometimes get the following error

Exception in defer callback: Error: Future resolved more than once
I20160114-15:20:42.939(-5)?     at Object.Future.return (/home/dpatte/.meteor/packages/meteor-tool/.1.1.10.6elsyt++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:226:10)
I20160114-15:20:42.939(-5)?     at TrackerInstance._runFlush (packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:186:1)
I20160114-15:20:42.939(-5)?     at packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:104:1
I20160114-15:20:42.939(-5)?     at packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:90:1
I20160114-15:20:42.939(-5)?     at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20160114-15:20:42.939(-5)?     at packages/meteor/timers.js:6:1
I20160114-15:20:42.939(-5)?     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)

I am using calls to Meteor.user().find and .findOne in my publish function. Could this be an issue?

server transform for cursors in autorun

Hi,

I was wondering if its possible to add a field to the result in the cursor in an autorun?

I tried with this package like so:

Meteor.publishTransformed("remarks", function () {
    this.autorun(function (computation) {
    return Remarks.find({}).serverTransform({extra: "hello"})
    })
})

The extra field is added when the query is not in the autorun function, but fails quietly if its in autorun. (autorun still works)

Its not very important because I can always do the extra work on the client. Just curious.

Error on Meteor Update 1.6.1 when using practicalmeteor:mocha

localCollectionLimit = new ReactiveVar(null);

Meteor 1.6.1 does not see the ReactiveVar inside the atmosphere package.
W20180225-07:01:23.344(-5)? (STDERR) ReferenceError: ReactiveVar is not defined
W20180225-07:01:23.344(-5)? (STDERR) at _fn (packages/peerlibrary_reactive-publish.js:421:5)

Issue with using observeChanges in autorun function

Hello, Before using meteor-reactive-publish, my publish function code looked like:

let handle = cursor.observe({
        added: doc => {
          this.added('placeholders', doc._id, transform(doc))
        },
        changed: (newDoc, oldDoc) => {
          this.changed('placeholders', oldDoc._id, transform(newDoc))
        },
        removed: doc => {
          this.removed('placeholders', doc._id)
        }
})

So some transformation function was used.
Then I added meteor-reactive-publish and wrapped this code in this.autorun. And now when I get DDP messages - first I get added message with some extra field from transform function, but then I get changed message that has cleared field with my extra field names and this fields are removing.
Снимок экрана 2019-10-03 в 12 08 18

I can't understand why this happens and how to transform data before publish to client?
Help please :)

Is this package meant to return multiple (nested) cursors like reywood:publish-composite can do?

Hi Mitar,

I heard roumors that this package is the better "replacement" for reywood:publish-composite, but could not figure out how to publish nested cursors, like I can do with "reywood:publish-composite".

How would you publish something like this with your package? OR is this package not the solution for the below useCase? This is "reywood:publish-composite"-code:

Meteor.publishComposite('posts.joined', {
    find: function() {
        return Posts.find({}, { limit: 600 });
    },
    children: [
        {
            find: function(post) {
                // Find post author. Even though we only want to return
                // one record here, we use "find" instead of "findOne"
                // since this function should return a cursor.
                return Authors.find(
                    { _id: post.authorId },
                    { limit: 1 })
            }
        },
        {
            find: function(post) {
                // Find top two comments on post
                return Comments.find(
                    { _id: { $in: post.commentIds } })
            },
        }
    ]
})

How would you do somethings like this with your package? Can you give an example?

Thanks a lot for your help!

Reactive app blocking errors

Hey man,

Great work on this package, just wanted to see if anyone else is starting to experience this issue.

Here is a log that I started to get often, and it never used to happen, but now it does for some reason.
I removed kadira packages, saw on another issue that was causing some problems.

The kadira packages I currently have.
kadira:blaze-layout
kadira:debug
kadira:flow-router

And when the error triggers, it feels random, I will edit/save the document and out of nowhere the log shows up and the document is gone from the client.

Here is a log of the issue.

Exception from sub sharedContacts id uMwMrYeBBkdJTARhH { stack: 'TypeError: Can\'t call method on
null\n    at TypeError (<anonymous>)\n    at module.exports
(/Users/pchubenko/.meteor/packages/ecmascript
runtime/.0.2.6.gitom7++os+web.browser+web.cordova/npm/node_modules/meteor-ecmascript
runtime/node_modules/core-js/modules/$.defined.js:3:28)\n    at module.exports
(/Users/pchubenko/.meteor/packages/ecmascript
runtime/.0.2.6.gitom7++os+web.browser+web.cordova/npm/node_modules/meteor-ecmascript
runtime/node_modules/core-js/modules/$.to-object.js:4:17)\n    at Function.keys
(/Users/pchubenko/.meteor/packages/ecmascript
runtime/.0.2.6.gitom7++os+web.browser+web.cordova/npm/node_modules/meteor-ecmascript
runtime/node_modules/core-js/modules/es6.object.keys.js:6:18)\n    at [object Object].cursorProto
(anonymous function) (packages/meteorhacks_kadira/lib/hijack/db.js:138:1)\n    at [object
Object].kadira_Cursor_observeChanges [as observeChanges] 
packages/meteorhacks_kadira/lib/hijack/set_labels.js:87:1)\n    at
Function.Mongo.Collection._publishCursor (packages/mongo/collection.js:312:1)\n    at [object
Object].Cursor._publishCursor (packages/mongo/mongo_driver.js:892:1)\n    at publishHandlerResult
(packages/peerlibrary_reactive-publish/packages/peerlibrary_reactive-publish.js:34:1)\n    at
Computation.<anonymous> (packages/peerlibrary_reactive-publish/packages/peerlibrary_reactive
publish.js:326:1)',

And this is how my pub/sub looks

Meteor.publish("sharedContacts", function () {
    this.autorun(function (computation) {
        var allSharedContacts = SharedContacts.find({sharedWith: this.userId, accepted: true}).fetch();
        return ContactData.find({_id: {$in: _.pluck(allSharedContacts, 'contactId')}, shared: true});
    });
});

Sub is at the router in lib directory

FlowRouter.subscriptions = function() {
    this.register('sharedContacts', Meteor.subscribe('sharedContacts'));
};

I also tried doing sub at the Template level, the error from sub looks like this.

 Exception from Tracker recompute function:
 TypeError: Can't call method on  null
     at TypeError (<anonymous>)
     at module.exports (/Users/pchubenko/.meteor/packages/ecmascript-runtime/.0.2.6.gitom7++os+web.browser+web.cordova/npm/node_modules/meteor-ecmascript-runtime/node_modules/core-js/modules/$.defined.js:3:28)
     at module.exports (/Users/pchubenko/.meteor/packages/ecmascript-runtime/.0.2.6.gitom7++os+web.browser+web.cordova/npm/node_modules/meteor-ecmascript-runtime/node_modules/core-js/modules/$.to-object.js:4:17)
     at Function.keys (/Users/pchubenko/.meteor/packages/ecmascript-runtime/.0.2.6.gitom7++os+web.browser+web.cordova/npm/node_modules/meteor-ecmascript-runtime/node_modules/core-js/modules/es6.object.keys.js:6:18)
     at [object Object].cursorProto.(anonymous function) (packages/meteorhacks_kadira/lib/hijack/db.js:138:1)
     at [object Object].kadira_Cursor_observeChanges [as observeChanges] (packages/meteorhacks_kadira/lib/hijack/set_labels.js:87:1)
     at [object Object].MeteorCursor._depend (packages/peerlibrary_reactive-mongo/packages/peerlibrary_reactive-mongo.js:46:1)
     at [object Object].MeteorCursor.(anonymous function) (packages/peerlibrary_reactive-mongo/packages/peerlibrary_reactive-mongo.js:71:1)
     at [object Object].cursorProto.(anonymous function) (packages/meteorhacks_kadira/lib/hijack/db.js:126:1)
     at [object Object].kadira_Cursor_fetch [as fetch] (packages/meteorhacks_kadira/lib/hijack/set_labels.js:75:1)

ReferenceError: ReactiveVar is not defined

Performed meteor add peerlibrary:reactive-publish then ran application and server side throws the following error:

W20220203-16:56:21.528(-7)? (STDERR) Debugger attached. W20220203-16:56:25.604(-7)? (STDERR) Waiting for the debugger to disconnect... W20220203-16:56:25.619(-7)? (STDERR) /Users/butlea1/.meteor/packages/meteor-tool/.2.5.3.1ssl86p.p3e7++os.osx.x86_64+web.browser+web.browser.legacy+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280 W20220203-16:56:25.619(-7)? (STDERR) throw(ex); W20220203-16:56:25.619(-7)? (STDERR) ^ W20220203-16:56:25.619(-7)? (STDERR) W20220203-16:56:25.619(-7)? (STDERR) ReferenceError: ReactiveVar is not defined W20220203-16:56:25.620(-7)? (STDERR) at _fn (packages/peerlibrary_reactive-publish.js:421:5) W20220203-16:56:25.620(-7)? (STDERR) at packages/peerlibrary_reactive-publish.js:1501:3 W20220203-16:56:25.620(-7)? (STDERR) at packages/peerlibrary_reactive-publish.js:1506:4 W20220203-16:56:25.620(-7)? (STDERR) at packages/peerlibrary_reactive-publish.js:1512:3 W20220203-16:56:25.620(-7)? (STDERR) at /Users/butlea1/Code/turboflowSystem/turboflow/.meteor/local/build/programs/server/boot.js:406:15 W20220203-16:56:25.620(-7)? (STDERR) at Array.forEach (<anonymous>) W20220203-16:56:25.620(-7)? (STDERR) at /Users/butlea1/Code/turboflowSystem/turboflow/.meteor/local/build/programs/server/boot.js:405:11 W20220203-16:56:25.621(-7)? (STDERR) at /Users/butlea1/Code/turboflowSystem/turboflow/.meteor/local/build/programs/server/boot.js:464:7 W20220203-16:56:25.621(-7)? (STDERR) at Function.run (/Users/butlea1/Code/turboflowSystem/turboflow/.meteor/local/build/programs/server/profile.js:280:14) W20220203-16:56:25.621(-7)? (STDERR) at /Users/butlea1/Code/turboflowSystem/turboflow/.meteor/local/build/programs/server/boot.js:463:13 => Exited with code: 1 => Your application is crashing. Waiting for file change.

Performed meteor add reactive-var and problem persists.

Performed meteor remove peerlibrary:reactive-publish and build ran correctly.

I'm running with Meteor version 2.5.3.

Any thoughts on the issue?

--Arch

this.added

Hi,

In your test cases you sometimes call this.added outside of your observeChanges function. I can't find this mentioned in your docs. Can you please explain what it does?

I have a feeling that it's going to be useful for my use case...

Tim

Testing with reactive-publish package

What is the best way to do publication testing with Meteor 1.3?

We are working with a publication test based on the Meteor docs, and they recommend using PublicationCollector, but it isn't working for us. Here is the error in my terminal:

Exception in queued task: TypeError: Cannot call method 'idStringify' of undefined
at PublicationCollector.publish.added (packages/peerlibrary_reactive-publish/packages/peerlibrary_reactive-publish.js:283:1)
at added (packages/mongo/collection.js:346:11)
at packages/mongo/observe_multiplex.js:183:30
at Function..each..forEach (packages/underscore/underscore.js:108:1)
at Object.task (packages/mongo/observe_multiplex.js:177:9)
at [object Object]._.extend._run (packages/meteor/fiber_helpers.js:147:1)
at packages/meteor/fiber_helpers.js:125:1

Thank you

After updating to Meteor 1.4.4.1 ReactiveVar is not defined

W20170425-11:37:15.200(-7)? (STDERR) ReferenceError: ReactiveVar is not defined
W20170425-11:37:15.201(-7)? (STDERR) at _fn (packages/peerlibrary_reactive-publish.js:426:32)
W20170425-11:37:15.201(-7)? (STDERR) at packages/peerlibrary_reactive-publish.js:1506:3
W20170425-11:37:15.201(-7)? (STDERR) at packages/peerlibrary_reactive-publish.js:1511:4
W20170425-11:37:15.201(-7)? (STDERR) at packages/peerlibrary_reactive-publish.js:1518:3

Error: Removed non-existent document

When removing a doc while using redis-oplog, or removing a doc from the DB externally to meteor (e.g. mongoclient or robomongo) we get the error:

Exception in queued task: Error: Removed nonexistent document f9fQjuKsW2JdA65Zs
I20170202-00:26:50.319(-5)?     at SessionCollectionView.removed (packages/ddp-server/livedata_server.js:202:17)
I20170202-00:26:50.319(-5)?     at Session.removed (packages/ddp-server/livedata_server.js:391:10)
I20170202-00:26:50.322(-5)?     at Subscription.removed (packages/ddp-server/livedata_server.js:1273:19)
I20170202-00:26:50.323(-5)?     at removed (packages/mongo/collection.js:352:11)
I20170202-00:26:50.323(-5)?     at callbacks.(anonymous function) (packages/peerlibrary_reactive-publish/server.coffee:36:24)
I20170202-00:26:50.324(-5)?     at packages/mongo/observe_multiplex.js:183:30
I20170202-00:26:50.325(-5)?     at Array.forEach (native)
I20170202-00:26:50.325(-5)?     at Function._.each._.forEach (packages/underscore.js:139:11)
I20170202-00:26:50.325(-5)?     at Object.task (packages/mongo/observe_multiplex.js:177:9)
I20170202-00:26:50.325(-5)?     at [object Object]._.extend._run (packages/meteor.js:807:18)
I20170202-00:26:50.326(-5)?     at packages/meteor.js:785:14

Propagate errors inside autoruns to the outside publish function

If computation throws an exception in later reruns, we should catch that and propagate it to the outside publish function which should throw an error and stop. For example, if some permission checks are done inside an autorun, and they fail, that should propagate out.

In general it seems no exception handling is done inside autorun. This is OK for the first run, but not for later.

@_idFilter unset?

I'm getting the following stack trace when calling @added from Meteor.publish, even without using @autorun.

Exception from sub ... id ... TypeError: Cannot call method 'idStringify' of undefined
   at publish.added (...\.meteor\local\build\programs\server\packages\peerlibrary_reactive-publish.js:305:33)

It's caused by this package, specifically server.coffee:128:

stringId = @_idFilter.idStringify id

I see that @_idFilter is supposed to be set here: https://github.com/meteor/meteor/blob/b13bed66c37ddd203e63fd4416a40e10333f2940/packages/ddp-server/livedata_server.js#L1006

Unlike #7, I'm not using Kadira... Here is a full list of packages I'm using:

accounts-password                   1.1.4  Password support for accounts
benjaminrh:jquery-cookie            0.1.0  A simple, lightweight jQuery ...
blaze-html-templates                1.0.1  Compile HTML templates into r...
bozhao:link-accounts                1.2.6* Meteor external service link ...
check                               1.1.0  Check whether a value matches...
chuangbo:marked                     0.3.5_1  A markdown parser and compi...
coffeescript                        1.0.11  Javascript dialect with fewe...
djedi:sanitize-html                 1.11.3  Wrapper for punkave's Saniti...
djedi:sanitize-html-client          1.11.2* Wrapper for punkave's Saniti...
edemaine:bootstrap-slider           1.0.1+ seiyria's bootstrap-slider pa...
ejson                               1.0.7  Extended and Extensible JSON ...
email                               1.0.8  Send email messages
force-ssl                           1.0.6  Require this application to u...
gcampax:accounts-dropbox            1.0.0  Accounts service for Dropbox
gcampax:dropbox-oauth               1.0.0  Login service for dropbox acc...
http                                1.1.1  Make HTTP calls to remote ser...
ian:accounts-ui-bootstrap-3         1.2.89  Bootstrap-styled accounts-ui...
iron:router                         1.0.12  Routing specifically designe...
jquery                              1.11.4  Manipulate the DOM using CSS...
logging                             1.0.8  Logging facility.
meteor-base                         1.0.1  Packages that every Meteor ap...
mizzao:bootstrap-3                  3.3.1_1  HTML, CSS, and JS framework...
mizzao:sharejs                      0.7.5  server (& client library) to ...
mizzao:sharejs-ace                  1.1.9* ShareJS with the Ace Editor
mnmtanish:iced                      1.1.0  Iced Coffee Script compiler a...
mobile-experience                   1.0.1  Packages for a great mobile u...
momentjs:moment                     2.12.0  Moment.js (official): parse,...
mongo                               1.1.3  Adaptor for using MongoDB and...
mquandalle:jade                     0.4.9  Jade template language
natestrauser:jquery-scrollto        1.4.7  jquery.scrollTo pacakged for ...
peerlibrary:reactive-publish        0.2.0  Reactive publish endpoints
peppelg:bootstrap-3-modal           1.0.4  Simple usage of bootstrap 3 m...
random                              1.0.5  Random number generator and u...
reactive-var                        1.0.6  Reactive variable
reload                              1.1.4  Reload the page while preserv...
sacha:spin                          2.3.1  Simple spinner package for Me...
session                             1.1.1  Session variable
silentcicero:jszip                  0.0.4  Create, read and edit .zip fi...
spacebars                           1.0.7  Handlebars-like template lang...
standard-minifiers                  1.0.2  Standard minifiers used with ...
tracker                             1.0.9  Dependency tracker to allow r...
underscore                          1.0.4  Collection of small helpers: ...
underscorestring:underscore.string  3.3.4  underscore.string (official):...
vsivsi:file-collection              1.3.1* Collections that efficiently ...

My server code looks like this. I even tried manually forcing @_idFilter to be set before @added gets called, but I get the same exception... And I tried console.log @ and indeed, _idFilter is properly set, but somehow @added isn't getting the right this?

  Meteor.publish 'messages.subscribers', (root) ->
    @_idFilter = idStringify: (x) -> x
    check root, String
    if canSee root, false, findUser @userId
      @added 'message.subscribers', root,
        subscribers: ['hi', 'there']
    @ready()

Support for Meteor < 1.2.x

I'm using matb33:collection-hooks in an app, and if I try to use autorun in a publish function I get the following:

Exception from sub foos id .............. TypeError: Object [object Object] has no method 'autorun'
at [object Object].<anonymous> (app/reactive-composite-hooks.js:35:10)
...

This happens in Meteor 1.1.x but not Meteor 1.2.x.

Subscriptions never complete

I am running meteor 1.2.
I have several publications which work well, but are not reactive.

Adding your package, with no other changes, has no effect (positive or negative) since I am not using autorun in my publications. My publications still work well, but not reactively

but once I modify my publications as follows
this.autorun() {
/* my old publication code */
}
The subscriptions never complete. My app sits with an idle indicator when I log in.

Also, in your documentation - what is the purpose of 'computation' that you pass as a parameter to autorun?

Bad interaction between this package and Kadira

Hey @mitar thanks for maintaining this package. Really cool use this.autorun on Meteor.publish.

Unfortunately I got an strange error:

error caught on publication:  null :  Cannot call method 'idStringify' of undefined

I see that you have used the method idStringify here

I'm using the publication with the this.autorun only in a ios-ddp.
The web app does not use and it shows only when I access it for the first time.

here is the publish:

Meteor.publish('time_ios', function () {
  this.autorun((comp) => {
    let isDST = moment().local().isDST();
    let randomId = Random.id();
    if (isDST) {
      let time = Time.findOne({group: 'firstSlotsGroup'});
      time.arrayDateSlots.forEach((dateSlots)=> {
        dateSlots.timeSlots.map((timeSlot)=> {
          timeSlot.time += 3600;
        });
      });
      this.added('time',randomId, time);
      return Time.find({_id: randomId});
    } else {
      return Time.find({});
    }
  });
});

As you can see it's a workaround for legacy apps =/
Thanks!

Re-using observers

This lib is a life-saver, thanks @mitar

We are now facing production issues with low observer re-use. Upon re-running the autorun in the publish function, is the previous observer removed (if not used) and the new one takes over?

Same question with peerlibrary:subscription-data

I don't know Meteor internals well enough to get the answer from the source codes of the packages

App with Flow Router requires page reload to publish

So, I have a subscription on my client...

Meteor.subscribe('posts');

and a publish on my server...

Meteor.publish('posts', function () {
    this.autorun(function (computation) {
        var author = Authors.findOne({userId: this.userId});
        return Posts.find({authorId: author._id});
    });
});

But, it doesn't return any post unless I reload the page in the browser.

The package overrides or ignores my Mongo poolSize setting

Hello, I'm using ChangeStreams in my app and to fix the performance issue with them I have to change poolSize option like this.

Mongo.setConnectionOptions({ poolSize: 300 });

It works fine, until I have your package installed. With it enabled poolSize rolls back to default 5 value.

Any ideas how to fix that?

Return values

Just discovered this great package which is perfect for subscribing to things a user has permission to access!

A small issue I encountered because I use CoffeeScript: the following code does not work.

Meteor.publish 'items', ->
  @autorun ->
    Items.find
      group: $in: Meteor.users.findOne(@userId).groups

The reason is that this.autorun returns something (a computation?), and in CoffeeScript, this gets automatically returned to Meteor.publish, which then complains "Publish function can only return a Cursor or an array of Cursors".

Not a big deal. I'm not sure whether you can/should modify Meteor.publish to allow another type of return value for this common case. But at least it might not hurt to mention this (common?) caveat for CoffeeScript users in the README.

how to achieve many-to-many

There is a need:
first query returns many items, every query result have many items from second query,
how to achieve this?
Thanks.

Collection2

Is there a way of getting this to work with Collection2 ?

I get the following error:

Exception from sub JobDetails id odpZRzshfNhQGvCoW { stack: 'TypeError: Object [object Object] has no method \'autorun\'
    at [object Object].Package (packages/job-details/job-details.js:540:1)
    at packages/matb33_collection-hooks/collection-hooks.js:275:1\n 

ReactiveVar is not defined

Hi, I was so happy when I saw the code example of this package:

var currentTime = new ReactiveVar(new Date().valueOf());

Meteor.setInterval(function () {
  currentTime.set(new Date().valueOf());
}, 1000); // ms

etc.

Now when I try to do just that, I always get the error ReferenceError: ReactiveVar is not defined in the server console.

The regular meteor reactive-var - package is in the package.js of the project.

The Meteor Version of this project is 1.3.5.1.

Is there something else I need / does this not work anymore?

I'm looking for a way to create regular self-updating publications, eg. check for any documents with a release date earlier than now.

Thanks and godspeed!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.