How do I know in client if MongoDB cluster has primary? - node.js

It is possible to connect to a MongoDB cluster which currently does not have by setting "connectWithNoPrimary" to "true". Then, however, it will still be possible to read but not write. I need a way to know if that's the case in an Express middleware. Sadly did not find a way to do that.
I know there is "mongoose.connection.readyState" which tells me if the connection is ready is there something similar to know if I can write?
Thanks!

Related

How to store a Net socket to a database in order to keep a node js app stateless?

My application is using Net API (https://nodejs.org/api/net.html) sockets and accept connections from clients that cannot use any other way to communicate than raw TCP sockets.
I'm trying to use pm2 with my app and clusterize it on all my cpus. Unfortunately this does not work since one process cannot use the in-memory stored sockets from any of the other processes.
I'm looking for a way to save each connections to a database, even an in-memory store would fit my needs for as long as i can call it and use it again from any other process.
Somebody else asked pretty much the same question here he was told that he should just use Redis to store the sockets. But the guy who answered apparently had no idea how to do that.
Question was asked here: Will PM2 work with Node.js net API?
My question is how could i do that ?
Actually i think this is just impossible. What i really need is a way to "recreate" the socket object through it's file descriptor number that could be save as an integer in the database, but again, this is getting really tricky. I'm kind of stuck here. Documentation says nothing about it.
Maybe there's another way to keep my app stateless ?
Thanks a lot for reading, i'll be glad if someone can help.
There are no methods to save a special type that is a resource.
In the database you can save the way how to re-created it (re-opene).
But I think that in your situation, a better solution will be to add an extra layer that will provide communication with a descriptor and all elements of the cluster. In the option you have: exchange events between node process or WebSockets.
What you can do is sharing file descriptors by passing them between the server processes using a Unix domain socket. Have a look at the usocket package which implements passing file descriptors for Node.js.

use custom mongoDB server instead of mongolab

Today i started migrating from firebase to mongoDB,
I used this tutorial and its all up and running http://thejackalofjavascript.com/re-architecting-a-firebase-app-in-node/
When checking the code i see there is a mongolab link connected to the code,
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
My question is: can i easily setup my own local database to use instead of this? and what packages i would need to do this?
The main reason for switching to mongo instead of firebase is the pricing, please take this into account.
Yes, of course you can.
First, install mongodb locally, to do that, follow the instructions for the distribution you're working on.
Then, make sure your mongodb service is running.
After that, on your database connection script, change the connection parameters.
I guess that you have something like this on your .js file:
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
Just to make a connection test, try to change it to:
mongo://localhost/test
After a successful connection, you can start to manage your database as you want.
As additional information, you don't have to specify user, password and a different port than the mongo's default, because you're using your local configuration, if you want to do so, you have to configure your mongodb server to make it work that way.
I'm working with mongoose ORM to manage a local database, and here's my connection function working.
Mongoose connection to local database
Hope this helps you.

Is it good practice to connect to the db multiple times?

I am using MongoDB, so I am connecting trough MongoClient.connect
but I have to use that, for every route where I want to work with the database.
Tried to preload it to an object, but then the changes are not visible, till the server is restarted. Right now, it's working properly, I am only a bit worried about the performance.
Is there a better way to do that?
THe correct answer is "it depends".
For a simple desktop application where your NodeJS program is the only client: sure. A persistent connection is fine.
For an enterprise application with 100s or 1000s of concurrent users each connecting independently: no, you probably do NOT want to hold the connection open "forever".
One possible solution for the latter scenario is Connection Pooling.
You should only need to connect to your DB once - when the server starts. As long as the server is running, the connection should persist. There is no reason to connect multiple times.

Disconnecting from Firebase 3.x in Node

I have a short running micro-service in NodeJS which needs to connect to a Firebase DB, do a few things, and then disconnect. The current code uses the firebase.goOffline() method as there was nothing else that looked anything like it would provide a graceful exit/tear down.
Having now looked at the documentation though it feels like this API call is not really achieving what I'm after. What is the right way to return the resources to the database once you're done with them?

Connection pooling in node-mongodb-native, when to call db.open and db.close

I've read most questions here about node-mongodb-native but I can't work out the standard practice as to when I should open/close a connection.
Some sources say open/close as needed, some say use one db instance throughout. Does node-mongodb-native support automatic connection pooling? If so, how do I use this?
I would really appreciate example code showing correct use of db.open and db.close in relation to, say, a login request.
I suggest to use generic-pool
It's very clear and pretty straightforward, you define how to open connection, how to close, and size of the pool. The module takes care of the rest, creating new connections as needed, and disposing unused connection after timeout you also select.
I use the module with every resource I need to pool, so I dont have to bother with custom pooling API every time.
This is the best answer I could find. Apparently, it works automatically, but I'm still figuring out the details.
Let me know if you find anything!
http://technosophos.com/node/255

Resources