Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB as Real-time database

I have an experience with real-time database firestore. Currently, I'm working on Ionic 3 app with MongoDB. On that app, we have to use pull to refresh feature to update the latest content. But if we have real-time DB then we don't need to have such feature. Due to above issue, my client now wants to use firestore. But the key problem where we have is data migration. That is MongoDB to firestore. Currently, this app is in production (i.e. app stores) and having over 500+ users. Because of that converting app to firestore will be a very difficult task. So my question here is, Can't we use real-time DB features with MongoDB?

Note: I use Nodejs/Express as Restfull api.

like image 217
Sampath Avatar asked Apr 01 '26 01:04

Sampath


1 Answers

What's your backend? How about using socket.io?

Since you're using MongoDB and Express already, here's a sample:

Server file:

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');
});

in your client:

<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
});
like image 187
wobsoriano Avatar answered Apr 02 '26 20:04

wobsoriano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!