神刀安全网

Parse Server Goes Realtime with Live Queries

One of the key concepts behind Parse is the Query, which lets you tell the server which objects you need. The Query is based on a "pull" model. Every time you want to get new data, you run another query. For some applications, like realtime games or messaging systems, it’s more convenient if you can get a constant stream of object updates.

To solve this problem, today we are introducing Parse Live Queries. To use live queries, you just construct a Query like you normally would and call subscribe() instead of find() . After that, whenever an object matching this query changes, the server will push the object data to you in realtime.

There is a catch – Live Queries only work with the open source Parse Server. They don’t work with apps that are still using theparse.com hosted service. So this is yet another good reason to migrate to Parse Server today. We strongly recommend you do this as soon as possible.

Setting up Live Queries

There are many ways to set up Live Queries with Parse Server–in the same process, in different processes, or with separate servers. The simplest way is to set it up in the same process as Parse Server. Add one line to your Parse Server setup to tell it which classes should use live queries:

 let api = new ParseServer({   ...,   liveQuery: {     classNames: ['Player']   } }); 

Then, it just takes a few lines of code to create the LiveQuery server:

 // app is the Parse Server instance  let httpServer = require('http').createServer(app); httpServer.listen(port); ParseServer.createLiveQueryServer(httpServer); 

Once your app gets larger, you might want a more complicated setup with more LiveQuery servers. You shouldn’t have to worry about that for a while, but there are several ways to set up additional LiveQuery servers. You can see more details about setup here .

We’ve also provided a complete example for you here .

Using Live Queries

Today we are providing a LiveQuery client in our main JavaScript SDK (v1.8) and a separate client package for iOS . Let’s use JavaScript for our example. In order to use a live query, you just subscribe() instead of calling find() on a regular Query.

 let query = new Parse.Query('Player'); query.equalTo('name', 'Mengyan'); let subscription = query.subscribe(); 

This subscription will receive updates for objects that match this query. For example, you can listen for the create event:

 subscription.on('create', (person) => {   console.log(person.get('favoriteAthlete')); }); 

After that, if someone creates a new Player object that matches this query, with data like:

 {   objectId: 'rj9f032j0k',   name: 'Mengyan',   favoriteAthlete: 'INnoVation',   league: 'Platinum' } 

Then the create event on the subscription will be triggered, and it will log INnoVation to the console.

There are other subscription events, like update , delete , enter , leave , and error . Check the documentation for a more in-depth explanation of when to use each one.

All Live Queries functionality is open source, both client and server. You can check out the LiveQuery server code here , Javascript client code here and iOS client code here . For the complete doc, you can check LiveQuery server here , Javascript clienthere and iOS client here . We know we haven’t built everything you might want in a realtime system. So if you have ideas for how to extend it, send in a pull request! Also, if you are still running an app on parse.com, don’t forget that you can only use Live Queries after you’ve migrated to the open source Parse Server . Thanks for reading!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Parse Server Goes Realtime with Live Queries

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮