简体   繁体   English

流星服务器端更新/插入与客户端更新/插入

[英]Meteor server-side update/insert vs client side update/insert

I have a question about the advantages vs disadvantages of an update/insert of a collection on the client vs server. 我对在客户端与服务器上更新/插入集合的优缺点有疑问。 For example say I have the method which takes a current player, sets him/her no longer as the current player and then creates a new current player. 例如,说我有一个方法,该方法采用一个当前玩家,不再将其设置为当前玩家,然后创建一个新的当前玩家。

  Meteor.methods({
       currentPlayer : function () {
              var id = Player.findOne({current:true})._id;
              Player.update(id, {$set:{current:false}});
              Player.insert({current:true});
  ...

What would be the advantages to doing this on the server vs doing the exact same thing on the client side: 与在客户端执行完全相同的操作相比,在服务器上执行此操作有什么好处:

  'click #add' : function () {
         var id = Player.findOne({current:true})._id;
         Player.update(id, {$set:{current:false}});
         Player.insert({current:true});
   ...

Maybe there aren't any inherently important differences or advantages to either technique. 也许这两种技术没有本质上的重要区别或优势。 However if there is I would like to be aware of them. 但是,如果有,我想知道它们。 Thanks for your input! 感谢您的输入!

The main difference is latency compensation. 主要区别是延迟补偿。

Under the hood, Player.update/insert/remove , uses a Meteor.call anyway. Player.update/insert/removePlayer.update/insert/remove仍然使用Meteor.call The difference is that it simulates the result of a successful operation on the browser before it has happened. 区别在于,它会在浏览器成功运行之前模拟其成功运行的结果。

So say your server is somewhere on the other side of the world where it has a 2-3 second latency. 因此,假设您的服务器位于世界另一端的某个地方,它的延迟为2-3秒。 If you update your player using Player.insert/update it would reflect instantly as if it was inserted and updated. 如果使用Player.insert/update更新播放器,它将立即反映出来,就好像它已插入并更新一样。 This can be make the UI feel responsive. 这可以使UI响应。

Using a Meteor.methods waits for the server to send an updated record, meaning when you update something it would take the 2-3 seconds to reflect on your UI. 使用Meteor.methods等待服务器发送更新的记录,这意味着当您更新某些内容时,将需要2-3秒的时间才能反映在用户界面上。

Using the method's you can be sure that the data has been inserted on the server at the cost of UI responsiveness. 使用方法可以确保以UI响应为代价将数据插入到服务器上。 (You could also use the Player.insert & Player.update callbacks for this too. (您也可以Player.insert使用Player.insertPlayer.update回调。

With Meteor.methods you can also simulate this same latency compensation effect by doing the same Meteor.method on the client side with the code that you would like to run to simulate latency compensation. 使用Meteor.methods您还可以通过在客户端使用要运行的模拟Meteor.method的代码来模拟此延迟补偿效果,以模拟延迟补偿。

There's a bit more details on the specifics on how to do this at the docs: http://docs.meteor.com/#meteor_methods 有关如何执行此操作的详细信息,请参见文档: http//docs.meteor.com/#meteor_methods

I think Akshat has some great points. 我认为Akshat有一些优点。 Basically there isn't a lot of difference in terms of latency compensation if you define the method on both the client and the server. 如果在客户端和服务器上都定义了方法,则基本上在延迟补偿方面没有太多差异。 In my opinion, there are a couple of reasons to use a method: 我认为,使用方法有两个原因:

  • The operation can only be completed on the server or it results in some side effect that only makes sense on the server (eg sending an email). 该操作只能在服务器上完成,否则会导致一些副作用,仅在服务器上才有意义(例如,发送电子邮件)。

  • You are doing an update and the permissions for doing the update are complex. 您正在执行更新,并且执行更新的权限很复杂。 For example maybe only the leader of a game can update certain properties of the players. 例如,也许只有游戏的领导者才能更新玩家的某些属性。 Cases like that are extremely hard to express in allow/deny rules, but are easy to write using methods. 这样的情况很难用允许/拒绝规则表达,但是很容易使用方法编写。

Personally, I prefer using methods in large projects because I find it's easier to reason about state mutations when all of the changes are forced to funnel through a small set of functions. 就我个人而言,我更喜欢在大型项目中使用方法,因为当所有更改都被迫通过一小组函数集中时,我发现更容易推断状态突变。

On the other hand, if you are working on a smaller project that doesn't have a lot of complex update rules, doing direct collection mutations may be a bit faster to write. 另一方面,如果您正在处理一个没有很多复杂更新规则的较小项目,则直接收集变量的编写可能会更快一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM