FireStore for Ruby-如何订阅文档更改

时间:2018-06-26 01:47:28

标签: ruby google-cloud-firestore

我想使用Firestore API for Ruby

订阅ruby的firestore文档中的更改

我可以使用以下代码获取文档参考和数据,但不确定如何订阅更改

# Firestore API
firestore = Google::Cloud::Firestore.new(project_id: 'project_id', credentials: './google-cloud-key.json')

# Get a reference to a document
ref = firestore.collection('collection_name').doc('id')

# Get the actual data in the document
snapshot = ref.get

data = snapshot.exists? ? snapshot.data : nil

# From this point on, how can I subscribe to changes in the document?

2 个答案:

答案 0 :(得分:0)

当我看着Cloud Firestore documentation for Firebase for getting realtime updates时,它表示以下内容:

  

注意:C#,Go,PHP,Python或 Ruby 客户端库中尚不支持实时监听器。

因此,Ruby似乎不支持实时更新/订阅。

答案 1 :(得分:0)

此功能最近已添加到Fire-store客户端库中。

您可以使用DocumentReference#listen收听文档中的更改

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

listener = nyc_ref.listen do |snapshot|
  puts "The population of #{snapshot[:name]} "
  puts "is #{snapshot[:population]}."
end

# When ready, stop the listen operation and close the stream.
listener.stop