Sampath
Sampath

Reputation: 65870

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.

Upvotes: 11

Views: 14406

Answers (2)

Cameron Michie
Cameron Michie

Reputation: 1

Ably now offers a MongoDB database connector which the MongoDB change streams feature to distribute changes from the database to clients over Ably Pub/Sub channels - see the Ably LiveSync docs for MongoDB. As such, subscribing to change events on the clientside is as easy as subscribing to an Ably channel:

// Instantiate the Realtime SDK
const ably = new Ably.Realtime('{API_KEY}');


// Get the channel to subscribe to
const channel = ably.channels.get('myDocuments');


// Subscribe to messages on the 'myDocuments' channel
await channel.subscribe((message) => {
  console.log('Received a change event in realtime: ' + message.data)
});

Upvotes: 0

wobsoriano
wobsoriano

Reputation: 13434

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

Upvotes: 14

Related Questions