最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

NodeJS中的Firestore实时更新连接

运维笔记admin10浏览0评论

NodeJS中的Firestore实时更新连接

NodeJS中的Firestore实时更新连接

我正在开发NodeJS Web应用程序以通过Admin SDK从Firestore DB接收实时更新。

这是Firestore对象的初始化代码。部署应用程序(在AWS Elastic Beanstalk上)后,它仅执行一次:

const admin = require('firebase-admin');

var serviceAccount = require('./../key.json');

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

var db = admin.firestore();

FUNC.firestore = db;

然后,我在websocket通信中使用此firestore对象将实时更新发送到浏览器。这个想法是使用服务器作为浏览器和Firestore之间的代理。

socket.on('open', function (client) {
    var query = FUNC.firestore.collection("notifications").doc(client.user.id.toString()).collection("global");
    query.onSnapshot(querySnapshot => {
        querySnapshot.docChanges().forEach(change => {
            client.send({ id: change.doc.id, body: change.doc.data(), type: change.type });
        });
    }, err => {
        console.log(`Encountered error: ${err}`);
    });
});

socket.on('close', function (client) {
    var unsub = FUNC.firestore.collection("notifications").doc(client.user.id.toString()).collection("global").onSnapshot(() => {
    });
    unsub();
});

它工作了一会儿,但是几个小时后,客户端停止接收onSnapshot()更新,并且一段时间后服务器记录了错误:Encountered error: Error: 10 ABORTED: The operation was aborted.

怎么了?我应该在每个连接上初始化firestore吗?是否存在生命周期错误?

谢谢

编辑(一个非常糟糕的解决方案)

我已经尝试为每个登录用户创建一个firebase-admin应用实例,并以此方式更改了代码

const admin = require('firebase-admin');

var serviceAccount = require('./../key.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

FUNC.getFirestore = function (user) {
  try {
    user.firebase = admin.app(user.id.toString());
    return user.firebase.firestore();
  } catch(e) {
    //ignore
  }

  var app = admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
  }, user.id.toString());

  user.firebase = app;

  return user.firebase.firestore();
}

FUNC.removeFirebase = function (user) {
  if (user.firebase) {
    user.firebase.delete();
  }
}

然后是套接字侦听器:

    self.on('open', function (client) {
        var query = FUNC.getFirestore(client.user).collection("notifications").doc(client.user.id.toString()).collection("global");
        query.onSnapshot(querySnapshot => {
            querySnapshot.docChanges().reverse();
            querySnapshot.docChanges().forEach(change => {
                client.send({ id: change.doc.id, body: change.doc.data(), type: change.type });
            });
        }, err => {
            console.log(`Encountered error: ${err}`);
        });
    });

    self.on('close', function (client) {
        var unsub = FUNC.getFirestore(client.user).collection("notifications").doc(client.user.id.toString()).collection("global").onSnapshot(() => {
        });
        unsub();
        FUNC.removeFirebase(client.user);
});

所以当客户端由于某种原因而断开连接时,服务器删除了它的Firebase应用程序,它可以工作,但是我注意到服务器上的huge内存泄漏,我需要一些帮助] >>

我正在开发一个NodeJS Web应用程序,以通过Admin SDK从Firestore DB接收实时更新。这是Firestore对象的初始化代码。部署应用程序后,它仅执行一次(在...

回答如下:

更新后的答案

发布评论

评论列表(0)

  1. 暂无评论