Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
How to use Firestore onSnapshot (for real-time update) on Flask?

I'm building an app with Google Cloud services, the application is deployed through Google App Engine.

I would like to show information in real-time with Flask through Firestore's onSnapshot() method, so i tried to use flask_socketio to send the information to the client, but it doesn't work inside on_snapshot.

from flask import Flask, render_template, request
from google.cloud import firestore
from flask_socketio import SocketIO, emit

db = firestore.Client()


app = Flask(__name__)
socketio = SocketIO(app)


@app.route("/", methods=["GET"])
def map():

    live_ref = db.collection("events").document("live_events").get()
    return render_template("index.html", rec=live_ref.to_dict()["displayed_events"])


@socketio.on("test", namespace="/")
def test():
    def on_snapshot(doc_snapshot, changes, read_time):
        for doc in doc_snapshot:
            print(f"Received document snapshot: {doc.to_dict()}")
        with app.app_context():
            emit("message", "test_message", namespace="/", broadcast=True)

    test_ref = db.collection("events").document("live_events")
    test_watch = test_ref.on_snapshot(on_snapshot)


if __name__ == "__main__":
    socketio.run(app, debug=True)

index.html

$(document).ready(function() {
   
       var socket = io.connect('http://127.0.0.1:5000');
   
       socket.on('connect', function() {
           socket.emit('test');
       });
   
       socket.on("message", function(msg) {
           $("#messages").append('<li>'+msg+'</li>');
       });
   
   });
   </script>
   <ul id="messages"></ul>

So i was wondering how the problem could be solved or what other methods/services could be used to be able to receive data changes in real-time from the db firestore and display them on the client side. I have seen that it is possible to use on_snapshot directly from the client, but i would like to avoid using the firebase console.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda