require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a document referencenyc_ref=firestore.doc"cities/NYC"listener=nyc_ref.listendo|snapshot|puts"The population of #{snapshot[:name]} is #{snapshot[:population]}."end# When ready, stop the listen operation and close the stream.listener.stop
Methods
#last_error
deflast_error()->Exception,nil
The most recent unhandled error to occur while listening for changes.
If an unhandled error has occurred the listener will attempt to
recover from the error and resume listening.
Returns
(Exception, nil) — error The most recent error raised.
Example
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a document referencenyc_ref=firestore.doc"cities/NYC"listener=nyc_ref.listendo|snapshot|puts"The population of #{snapshot[:name]} is #{snapshot[:population]}."end# If an error was raised, it can be retrieved here:listener.last_error#=> nil# When ready, stop the listen operation and close the stream.listener.stop
#on_error
defon_error(&block){|error|...}
Register to be notified of errors when raised.
If an unhandled error has occurred the listener will attempt to
recover from the error and resume listening.
Multiple error handlers can be added.
Yields
(callback) — The block to be called when an error is raised.
Yield Parameter
error (Exception) — The error raised.
Raises
(ArgumentError)
Example
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a document referencenyc_ref=firestore.doc"cities/NYC"listener=nyc_ref.listendo|snapshot|puts"The population of #{snapshot[:name]} is #{snapshot[:population]}."end# Register to be notified when unhandled errors occur.listener.on_errordo|error|putserrorend# When ready, stop the listen operation and close the stream.listener.stop
#stop
defstop()
Stops the client listening for changes.
Example
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a document referencenyc_ref=firestore.doc"cities/NYC"listener=nyc_ref.listendo|snapshot|puts"The population of #{snapshot[:name]} is #{snapshot[:population]}."end# When ready, stop the listen operation and close the stream.listener.stop
#stopped?
defstopped?()->Boolean
Whether the client has stopped listening for changes.
Returns
(Boolean)
Example
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a document referencenyc_ref=firestore.doc"cities/NYC"listener=nyc_ref.listendo|snapshot|puts"The population of #{snapshot[:name]} is #{snapshot[:population]}."end# Checks if the listener is stopped.listener.stopped?#=> false# When ready, stop the listen operation and close the stream.listener.stop# Checks if the listener is stopped.listener.stopped?#=> true
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-09 UTC."],[],[],null,["# Cloud Firestore API - Class Google::Cloud::Firestore::DocumentListener (v3.1.0)\n\nVersion latestkeyboard_arrow_down\n\n- [3.1.0 (latest)](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-DocumentListener)\n- [3.0.0](/ruby/docs/reference/google-cloud-firestore/3.0.0/Google-Cloud-Firestore-DocumentListener)\n- [2.16.1](/ruby/docs/reference/google-cloud-firestore/2.16.1/Google-Cloud-Firestore-DocumentListener)\n- [2.15.1](/ruby/docs/reference/google-cloud-firestore/2.15.1/Google-Cloud-Firestore-DocumentListener)\n- [2.14.0](/ruby/docs/reference/google-cloud-firestore/2.14.0/Google-Cloud-Firestore-DocumentListener)\n- [2.13.1](/ruby/docs/reference/google-cloud-firestore/2.13.1/Google-Cloud-Firestore-DocumentListener)\n- [2.12.0](/ruby/docs/reference/google-cloud-firestore/2.12.0/Google-Cloud-Firestore-DocumentListener)\n- [2.11.0](/ruby/docs/reference/google-cloud-firestore/2.11.0/Google-Cloud-Firestore-DocumentListener)\n- [2.10.1](/ruby/docs/reference/google-cloud-firestore/2.10.1/Google-Cloud-Firestore-DocumentListener)\n- [2.9.1](/ruby/docs/reference/google-cloud-firestore/2.9.1/Google-Cloud-Firestore-DocumentListener)\n- [2.8.0](/ruby/docs/reference/google-cloud-firestore/2.8.0/Google-Cloud-Firestore-DocumentListener)\n- [2.7.2](/ruby/docs/reference/google-cloud-firestore/2.7.2/Google-Cloud-Firestore-DocumentListener)\n- [2.6.6](/ruby/docs/reference/google-cloud-firestore/2.6.6/Google-Cloud-Firestore-DocumentListener) \nReference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::DocumentListener.\n\nDocumentListener\n----------------\n\nAn ongoing listen operation on a document reference. This is returned by\ncalling [Google::Cloud::Firestore::DocumentReference#listen](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-DocumentReference#Google__Cloud__Firestore__DocumentReference_listen_instance_ \"Google::Cloud::Firestore::DocumentReference#listen (method)\"). \n\nInherits\n--------\n\n- Object \n\nIncludes\n--------\n\n- MonitorMixin\n\nExample\n-------\n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a document reference\nnyc_ref = firestore.doc \"cities/NYC\"\n\nlistener = nyc_ref.listen do |snapshot|\n puts \"The population of #{snapshot[:name]} is #{snapshot[:population]}.\"\nend\n\n# When ready, stop the listen operation and close the stream.\nlistener.stop\n```\n\nMethods\n-------\n\n### #last_error\n\n def last_error() -\u003e Exception, nil\n\nThe most recent unhandled error to occur while listening for changes.\n\n\n\u003cbr /\u003e\n\nIf an unhandled error has occurred the listener will attempt to\nrecover from the error and resume listening. \n**Returns**\n\n- (Exception, nil) --- error The most recent error raised.\n**Example** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a document reference\nnyc_ref = firestore.doc \"cities/NYC\"\n\nlistener = nyc_ref.listen do |snapshot|\n puts \"The population of #{snapshot[:name]} is #{snapshot[:population]}.\"\nend\n\n# If an error was raised, it can be retrieved here:\nlistener.last_error #=\u003e nil\n\n# When ready, stop the listen operation and close the stream.\nlistener.stop\n```\n\n### #on_error\n\n def on_error(&block) { |error| ... }\n\nRegister to be notified of errors when raised.\n\n\nIf an unhandled error has occurred the listener will attempt to\nrecover from the error and resume listening.\n\n\u003cbr /\u003e\n\nMultiple error handlers can be added. \n**Yields**\n\n- (callback) --- The block to be called when an error is raised. \n**Yield Parameter**\n\n- **error** (Exception) --- The error raised. \n**Raises**\n\n- (ArgumentError)\n**Example** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a document reference\nnyc_ref = firestore.doc \"cities/NYC\"\n\nlistener = nyc_ref.listen do |snapshot|\n puts \"The population of #{snapshot[:name]} is #{snapshot[:population]}.\"\nend\n\n# Register to be notified when unhandled errors occur.\nlistener.on_error do |error|\n puts error\nend\n\n# When ready, stop the listen operation and close the stream.\nlistener.stop\n```\n\n### #stop\n\n def stop()\n\nStops the client listening for changes.\n**Example** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a document reference\nnyc_ref = firestore.doc \"cities/NYC\"\n\nlistener = nyc_ref.listen do |snapshot|\n puts \"The population of #{snapshot[:name]} is #{snapshot[:population]}.\"\nend\n\n# When ready, stop the listen operation and close the stream.\nlistener.stop\n```\n\n### #stopped?\n\n def stopped?() -\u003e Boolean\n\nWhether the client has stopped listening for changes. \n**Returns**\n\n- (Boolean)\n**Example** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a document reference\nnyc_ref = firestore.doc \"cities/NYC\"\n\nlistener = nyc_ref.listen do |snapshot|\n puts \"The population of #{snapshot[:name]} is #{snapshot[:population]}.\"\nend\n\n# Checks if the listener is stopped.\nlistener.stopped? #=\u003e false\n\n# When ready, stop the listen operation and close the stream.\nlistener.stop\n\n# Checks if the listener is stopped.\nlistener.stopped? #=\u003e true\n```"]]