MongoDB as Real-time database - mongodb

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.

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

Related

What is mongodb equivalent for firebase onSnapshot to get realtime updates

For firebase, there is onSnapshot to get new messages in a chat application and its written in the client side code inside useEffect.
Purpose is - Let the server tell the client of new messages instead of client asking the server every second.
useEffect(() =>
onSnapshot(query(collection(db, 'matches', linkDetails.id, 'messages'), orderBy('timestamp', 'desc')),
snapshot => setMessages(snapshot.docs.map(doc => ({
id: doc.id,
...doc.data()
}))))
, [linkDetails, db])
What is the equivalent for mongodb to get new messages as soon as new messages are sent/received?
I have the messages in api - /api/messages/get which is the backend.
This is for react native and I do not use nextjs which has UseSWR.

unable to save data to local MongoDB (React native)

I am trying to use local MongoDB for the case when users don't have internet access. The real-time data is stored on firebase. When I tried to save the firebase data to the local MonogoDB, I got an error that mongoose.connect is not a function. Here is the code.
Aims to have a MongoDB database come along with the App, so that the client can get the data even there is no internet. How could I implement the save data to local MonogoDB in react native app? Thanks for your help!
class writeDataToMonogo extends React.component{
..........
insertData(){
const mongoose = require('mongoose');
const url = 'mongodb://.........';
mongoose.connect(url, {useNewUrlParser: true});
const conn = mongoose.connection;
conn.on('connected', function() {
console.log('database is connected successfully');
});
conn.on('disconnected',function(){
console.log('database is disconnected successfully');
})
conn.on('error', console.error.bind(console, 'connection error:'));
....
Mongoose is used on the server side to connect to mongodb.
You should not use it directly from react native.
Therefore, if the client has no internet access it won't be able to reach any server.
If you want to store offline data for later use, try using AsyncStorage:
https://github.com/react-native-async-storage/async-storage

Why is the MongoDB constant in index.js not required when using Mongoose?

As an aspiring developer, I was watching a video about mongoose on YouTube and noticed that the developer removed the MongoDB constant variable requiring MongoDB and just created one for Mongoose.
They say its an object modeling tool so if it's a tool for MongoDB shouldn't the MongoDB variable still be required in the file with the variables and routers?
I will admit I don't have good backend knowledge as a beginner so if you can explain in layman's terms that would be great.
Since mongoose being an ODM (Object Document Mapper) specifically for mongodb, you only need a connection to a particular database of mongo to be defined via mongoose...And the rest of the work would be done by mongoose. ( the only thing that still needs beside the mongoose connection is your mongo instance running in the background or whatever you're using specifically !) If you want to know in detail, read this article by Jamie Munro -> https://code.tutsplus.com/articles/an-introduction-to-mongoose-for-mongodb-and-nodejs--cms-29527 .. Hope this helps :)
Mongoose connection to mongodb
import mongoose from 'mongoose';
mongoose.Promise = global.Promise;
mongoose.set('useFindAndModify', false);
// mentioning url for specific mongo database along with the port
let DB_URL = 'mongodb://localhost:27017/db_name';
// creating a connection for mongoose to act to specific db
var connection = mongoose.createConnection(DB_URL,{ useNewUrlParser: true });
// if error occurs, then create connection or else connect directly
try {
mongoose.connect(DB_URL, { useNewUrlParser: true });
} catch (err) {
mongoose.createConnection(constants.DB_URL);
}
// consoling the success message if connection is opened or else displaying error if error is present
mongoose.connection
.once('open', () => console.log('MongoDB Running'))
.on('error', e => {
throw e;
});

Is it possible to export pubnub chat messages to a postgresql database?

I am prototyping a mobile app and i want to make it quickly. For that purpose, i am using pubnub chat engine.
However, i plan to migrate to a postgresql database after the end of the beta test. But i don't want to lose the existing data already stored in pubnub. Are there some possibilities to export my chat data to my own postgreSQL database ?
Export PubNub Chat Messages to your PostgeSQL Database
While many approaches exist. One is best. Using PubNub Functions. You will asynchronously save messages reliably to your Database. Using an OnAfter Publish Event. Your database needs to be accessible via a secured HTTPS endpoint.
Stackoverflow Answer: PubNub: What is the right way to log all published message to my db - You will want to Save your JSON Messages to a Private Database using the method described in the links. Example code referenced below in this post.
export default request => {
const xhr = require('xhr');
const post = { method : "POST", body : request.message };
const url = "https://my.company.com/save";
// save message asynchronously
xhr.fetch( url, post ).then( serverResponse => {
// DB Save Success!
}).catch( err => {
// DB Save Failed! handle err
});
return request.ok();
}

Meteor mocha test subscriptions

I'm currently working on writing client-side tests for my application using practicalmeteor:mocha. Therefor I need to insert a record into a mongo collection before every step.
Following the concepts of meteor, I wrote some meteor methods to insert/update/delete the records. If I want to test the expected behaviour, I assume I have to call the appropiate method (to insert a record for example) and then have to wait for the subscription to synchronize the record to the client so I can verify the insert.
I tried a few things but none of them seems to work:
describe('a topic', function() {
beforeEach(async function(done) {
let res = await new Promise((resolve, reject) => {
Meteor.call('topics.createNew', function(err, res) {
if(err) return reject(err);
resolve(res); //res is the _id from the generated record
});
});
//subscribe to single record (the one just created)
let subscription = Meteor.subscribe('topics.details', res);
//i assume this runs every time the publish function calls ready and therefor is also run when the new record is published
Tracker.autorun((computation) => {
let count = TopicsCollection.find({}).count();
//console.log('topic count: ' + count);
if(subscription.ready() && count === 1) {
computation.stop();
done();
}
});
});
});
I logged the counted documents and had luck when i wrapped the autorun-Funktion into a Promise but in neither of both cases, done() was called causing a timeout in mocha.
I already had a look on this question but I think it doesnt really help in my situation since I wait for every possible callback and I'm working with a plain Collection and the guy in the question above uses an accounts-package.
Can anyone suggest a better/working solution for this problem?
Thanks in advance! :D
Summarizing your current setup:
calling a method on the client
insert some doc on the server
subscribe to the pub from the client
verify inserts by the subscribed data
Which is a big blackbox that is on the scope here. Additionally you are testing things, that are already tested by the Meteor core team: integration tests of Methods and Publications between server and client.
To prevent waste of time, you can split up your setup into two tests:
Test the Meteor method as a unit
Test the Meteor publication as a unit
If those tests are defining the intended behavior with correct permission levels, you can assume, that if you are a logged in user with the correct permission level subscribing to your publication, that this user will get the resulting behavior as you have tested in the units. As I pointed out before, the pub/sub system should be assumed as 'working' by definition.
1. Test the method, server side only
Use hwillson:stub-collections to get a 'mock' of your collection, use practicalmeteor:sinon to mock your Meteor.user() object to test different permission levels.
2. Test the publication, server side only
Use johanbrook:publication-collector to test if your publication is running correctly and dburles:factory create mocks of your collection's data.
No need then for a complex test setup on the client side.