MongoDB 作为实时数据库

Sam*_*ath 3 real-time mongodb firebase ionic3 google-cloud-firestore

我有实时数据库 Firestore 的经验。目前,我正在使用 MongoDB 开发 Ionic 3 应用程序。在那个应用上,我们必须使用 pull to refresh 功能来更新最新的内容。但是如果我们有实时数据库,那么我们就不需要这样的功能。由于上述问题,我的客户现在想要使用 firestore。但我们面临的关键问题是数据迁移。那是 MongoDB 到 Firestore。目前,这个应用程序正在生产中(即应用程序商店)并且拥有超过 500 多个用户。因为将应用程序转换为 firestore 将是一项非常艰巨的任务。所以我的问题是,我们不能在 MongoDB 中使用实时数据库功能吗?

注意:Nodejs/Express用作 Restfull api。

wob*_*ano 8

你的后台是什么?使用socket.io怎么样?

由于您已经在使用 MongoDB 和 Express,这里有一个示例:

服务器文件:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/api/add', function(req, res){
    db.collection('quotes').save(req.body, (err, result) => {
        if (err) return console.log(err)

        // send everyone the added data
        io.emit('ADDED_DATA', req.body);
    });
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});
Run Code Online (Sandbox Code Playgroud)

在您的客户中:

<script src="/socket.io/socket.io.js"></script>

const socket = io('http://localhost:3030'); //ip and port of server

socket.on('ADDED_DATA', (data) => {
    // manipulate data
    // push to current list
    // or whatever you want
});
Run Code Online (Sandbox Code Playgroud)

  • 但这不能跨实例扩展?例如,如果您在 App Engine 上部署节点,它只会向部分用户发出警报,而不是向所有用户发出警报。 (3认同)