MeetorJavaScript 框架是如何工作的?

我偶然发现了 流星,虽然它看起来很令人兴奋,但我想知道它是如何工作的。我的意思是传统的 Web 应用程序是这样工作的: 服务器上有脚本,它们从数据库中获取数据并动态地将其添加到网页中,用户提交的数据通过其他脚本添加到数据库中。

但是这些东西在流星中是如何工作的呢? 流星的不同部分是如何相互关联的呢?

30681 次浏览

Disclaimer: This answer describes Meteor, JavaScript client library for Meteor Server. It was originally added due to ambiguity in the question, and may serve the purpose of clarifying similar ambiguities faced by the visitors searching for similar answers, but unsure about the difference.

To read about Meteor JavaScript framework, please see this answer by xer0x.

As mentioned on the Meteor Server's documentation, Meteor is an implementation of Comet. Comet in turn is a counterpart of AJAX.

In case of AJAX, you usually make a request when the client sees a need to do that. To pull updates from the server, you will need to call the server eg. every 5 seconds.

In case of Comet, the update from the server comes faster, because the connection is persistent. The connection is established by client, as in AJAX, but the server does not respond until it has some update or it reaches execution limit (scripts on the server may have execution limits).

In case of Meteor you just get constant stream of data that needs some specific server-side code (like Meteor Server) and appropriate code on the client (in this case it looks like it is Meteor class).

Meteor is a framework that elegantly updates HTML in realtime.

The beauty of Meteor is that you only need to create the templates and the data models. The rest of the usual boilerplate code is hidden away. You don't need to write all the sync-ing code.

The key pieces of Meteor could be built yourself using these pieces:

  • It provides templating that updates automatically when your data models do. This is normally done using Backbone.js, Ember.js, Knockout.js, or another tool.

  • The client/server messaging is done via websockets using something like socks.js or socket.io.

  • The client side connection to MongoDB is really cool. It replicates the MongoDB-server driver into the client. Unfortunately, last I checked, they were still working on securing this database connection.

  • The latency compensation is simply updating the client-side model first, then sending the update to the server-server.

There may be other neat pieces to that you can find on the Meteor site, or on GitHub.

All the magic with the live data updating is happening because of the dependency tracking system. An explanation of how it works can be found at the Tracker section of the documentation.