如何通过RPC监听Firestore [英] How to listen to firestore through RPC

查看:246
本文介绍了如何通过RPC监听Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想收听Firestore中的实时变化,并且只允许我使用Go.由于Go的firestore SDK没有任何选项可以监听实时更改,因此我决定使用firestore v1beta1 sdk.

I want to listen to real time changes in firestore and I am also only allowed to use Go. Since firestore SDK for Go doesn't have any option to listen for real time changes, I decided to use the firestore v1beta1 sdk.

我已经编写了以下代码来做到这一点

I have written the following code to do that

func TestRPCHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
c, err := firestore.NewClient(context.Background())

databaseName := "projects/[project_name]/databases/(default)"
if err != nil {
    panic(err)
}

stream, err := client.Listen(context.Background())
if err != nil {
    panic(err)
}

request := &firestorepb.ListenRequest{
    Database:             databaseName,
    TargetChange:         &firestorepb.ListenRequest_AddTarget{
        AddTarget: &firestorepb.Target{
            TargetType:           &firestorepb.Target_Documents{
                Documents: &firestorepb.Target_DocumentsTarget{
                    Documents:    []string{"projects/[project_name]/databases/(default)/[collection_name]"} ,
                },
            },
        },
    },
}

if err := stream.Send(request); err != nil {
    panic(err)
}

if err := stream.CloseSend(); err != nil {
    panic(err)
}

for {
    resp, err := stream.Recv()
    if err == io.EOF {
        break
    }
    if err != nil {
        panic(err)
    }
}

   }

执行此操作时,代码不会检测到我在数据库中手动带来的任何更改. stream.Recv()仅返回EOF并立即退出.我什至尝试通过添加time.Sleep()手动等待,但这也无济于事.

When I am doing this, the code does not detect any changes I bring about manually in the database. stream.Recv() just returns EOF and exits immediately. I even tried manually waiting by adding time.Sleep() but that does not help either.

推荐答案

您不需要Beta版SDK或黑客程序就可以实现这一目标,我找到了解决方案,实际上很简单.

You don't need the beta SDK or hacks to make this happen, I found the solution, it's pretty easy actually.

https://firebase.google.com/docs/firestore/query -data/listen 文档没有包含Go的示例.

The https://firebase.google.com/docs/firestore/query-data/listen documentation does not contain an example for Go.

Go的firestore客户端API的源代码具有未导出的watchStream,我们无法直接使用它:

The source code of the firestore client API for Go has an unexported watchStream which we cannot directly use: https://github.com/googleapis/google-cloud-go/blob/master/firestore/watch.go#L130

对存储库的深入搜索显示,它实际上已在DocumentSnapshotIterator和QuerySnapshotIterator上使用:

Deep search of the repository shows that this is actually used on the DocumentSnapshotIterator and QuerySnapshotIterator at: https://github.com/googleapis/google-cloud-go/blob/master/firestore/docref.go#L644 and: https://github.com/googleapis/google-cloud-go/blob/master/firestore/query.go#L716.

Collection包含一个Snapshots方法,该方法返回我们想要的快照迭代器,这很容易,我们只需通过其Next方法进行一次简单的循环即可.

The Collection contains a Snapshots method which returns the snapshot iterator that we want, after that all is easy, we just make an infivitive loop through its Next method.

示例:

cols, err := client.Collections(context.Background()).GetAll()

for _, col := range cols {
    iter := col.Snapshots(context.Background())
    defer iter.Stop()

    for {
        doc, err := iter.Next()
        if err != nil {
            if err == iterator.Done {
                break
            }
            return err
        }

        for _, change := range doc.Changes {
            // access the change.Doc returns the Document,
            // which contains Data() and DataTo(&p) methods.
            switch change.Kind {
            case firestore.DocumentAdded:
                // on added it returns the existing ones.
                isNew := change.Doc.CreateTime.After(l.startTime)
                // [...]
            case firestore.DocumentModified:
                // [...]
            case firestore.DocumentRemoved:
                // [...]
            }
        }
    }
}

您,Gerasimos Maropoulos又名 @kataras

Yours, Gerasimos Maropoulos aka @kataras

这篇关于如何通过RPC监听Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆