This is essentially down to two issues: firstly, that the implementations of setInterval
and clearInterval
are different on the client (at least in Chrome) and in Node, and secondly that you can't serialise functions in BSON, which means that all your methods, and properties containing methods, are being dropped from the object when you try to insert it as a document on the server. That's why the object you subsequently retrieve is so much simpler/smaller, and why you can't pass it to clearInterval
, as it's missing most of the required information.
If you log the return value of setInterval
in the client, you'll notice that it's just an integer, which can of course be serialised, and so you get exactly the same thing out of the MongoDB as you put in, and clearInterval
works fine. I am no expert at all on the client implementation of set...
and clearInterval
, but frankly a four-digit integer doesn't seem particularly robust for this purpose, although it does have some advantages that you've identified.
In summary, I don't think you're going to be able to go about things the way in which you're attempting on the server, unless somebody else can think of a smart way of serialising the part of the interval
object which is required for clearing it and rebuilding an appropriate object once it's been retrieved, but this requires more knowledge of Node than I possess. Otherwise, I think you have two options:
- Simply store your intervals in memory in some kind of vanilla javascript object.
- Use an alternative object for timing which would allow you to store a serialisable identifier. I have used the
meteor-cron
package in the past, albeit briefly, and it may be worth a look to see if this is possible using that package or some derivative thereof.