Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Database Reference.listen() is very slow for large responses

Open
#599 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
database

Research direction

Start with the Firebase Admin SDK's sseclient implementation used by Reference.listen(), then compare its response-reading behavior with the linked btubbs/sseclient.py. Reproduce the issue using the provided Raspberry Pi test and confirm that large listen() responses no longer take about 10 seconds or use nearly 100% CPU, while the callback still receives the complete event.

Written by the indexing model from the issue text.

Description

api: database
Describe your environment
  • Operating System version: Linux raspberrypi 4.19.97+ (On a Raspberry Pi Zero W)
  • Firebase SDK version: 5.2.0
  • Firebase Product: database
  • Python version: 3.7.3
  • Pip version: 18.1
Describe the problem

Reading a large event in response to calling Reference.listen() is extremely slow on a Raspberry Pi Zero. This looks very similar to https://github.com/nhorvath/Pyrebase4/issues/45 and I noticed that the sseclient here is based on that from Pyrebase.

From reading the sseclient code, the Firebase Admin SDK seems to have solved the quadratic regex scanning issue that the sseclient in Pyrebase4 has, but it still reads the response one character at a time, which I suspect is the root issue. It looks like this is solved in https://github.com/btubbs/sseclient/blob/master/sseclient.py by reading in chunks when short reads are supported.

Steps to reproduce:

Modify the test code below to add a working databaseURL and run it. On my Raspberry Pi Zero W, it outputs:

0 ms: Initializing app...
1 ms: Getting DB reference...
17 ms: Putting 20000 bytes...
1122 ms: Getting data...
1347 ms: Got 20000 bytes with get().
1349 ms: Listening for data...
11686 ms: Got 20000 bytes with listen().

That is, listen() takes about 10 000 ms (with close to 100% CPU usage) to get and read the response, while get() takes 200 ms.

Relevant Code:
import firebase_admin
import firebase_admin.db
import sys
import time

count = 2000
nonsense = "abcedefghi"
start_time = time.time()

def log(msg):
  t = int(1000 * (time.time() - start_time))
  print(str(t) + " ms: " + msg)

log("Initializing app...")
firebase_admin.initialize_app(options={'databaseURL': 'https://XXXXXXXXXX.firebaseio.com/'})

log("Getting DB reference...")
ref = firebase_admin.db.reference("bringert_test_streaming_speed")

data = nonsense * count

log("Putting " + str(len(data)) + " bytes...")
new_data=ref.push(data)

log("Getting data...")
log("Got " + str(len(new_data.get())) + " bytes with get().")

def callback(event):
  if event.event_type == "put":
    log("Got " + str(len(event.data)) + " bytes with listen().")
    sys.exit(0)

log("Listening for data...")
new_data.listen(callback)
Dominant language
Python
Stars
1.2k
Forks
359
Avg merge
3d 9h
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from firebase/firebase-admin-python

All issues in firebase/firebase-admin-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.