Connecting to Redis databases via Node in Heroku - node.js

About Heroku: App is Nodejs, Express, WebSockets ...
I'm testing an app out on a small free Heroku instance.
I have my own Redis database instances through OpenRedis and RedisToGo.
Those have not been provisioned through Heroku addons but directly from the providers.
I connect to them fine when running locally. What special do I need to do to connect to them through Heroku?
I was kind of hoping to avoid special Heroku-specific environment variables, but am happy to use them if necessary.
But I definitely need to avoid the necessity of provisioning the Redis addons through Heroku.
Again, I have already my database instances provisioned directly with the relevant providers.
Everything works fine locally under NODE_ENV=production npm start, which indicates to my app that it should connect to the remote Redis instances (RedisToGo and/or OpenRedis) instead of the local Redis server.
I'm using Redis for at least three things:
Session store :: Connect-Redis :: This works fine locally (app server running locally but connecting to remote Redis instance) and also seems to work on Heroku-- I've checked by flushing the database and then pushing a slightly new version to Heroku and the session variable key is refreshed.
Database :: Node-Redis :: This works fine locally also (also app server running locally but connecting to remote Redis instance for database), but does not seem to be working on Heroku.
WebSockets Transport Pub/Sub :: I haven't tested this thoroughly. I'm guessing it's working but it's not important for this question.
What is most interesting to me,
is that the Heroku instance is able to connect to the Redis instance via the Connect-Redis client, but is not able to connect to the same Redis instance via the database client. Whereas my locally running version is able to connect to the same (remote) instance via both of these clients. (I have verified these findings through terminal clients, using flushall command and waiting for keys to be refreshed.)
Any insight would be greatly appreciated.
I'll post my edits and updates via comments below because they're timestamped.

It turns out we can't connect to a database (not sure why because I don't know the internals of Heroku's config over AWS) the 'normal' way through Heroku; we need to set the environment variable with the heroku config:set command. OpenRedis provides a terminal ready string, can probably put something similar together for self-provisioned RedisToGo instances, though I haven't done it yet.

Related

AWS Document DB connection from Nodejs

I am working on a nodejs express app, which will finally be deployed to aws ec2 instance.
This nodejs app connects to aws document db and fetches some data. Now we know that to connect to aws doc db from the local machine (that is outside VPC) we need to use ssh tunnelling.
Now the question I have is, what is the good practice to use MongoClient in this situation, where locally we need to do ssh tunnelling but on production, we can skip this step. One can use env variables to do this, but I want to know what practise is widely accepted.

Connecting to a mongoDB with a TCP prefix on a NodeJS application

I created a NodeJS app that connects with a MongoDB and it seems to work locally.
Now, after hosting the NodeJS application on a remote machine, I am trying to connect to a mongoDB that was already created on that machine. When I print out some of the environment variables, the only one I see of relevance seems to be :
MONGODB_PORT: 'tcp://172.30.204.90:27017',
I tried connecting like I usually do with
mongoose.connect('mongodb://localhost:27017/metadata/')
and replacing it with mongoose.connect('tcp://172.30.204.90:27017/metadata') but I get an error that says my URI needs to start with 'mongodb'.
So I tried replacing it with mongoose.connect('mongodb://172.30.204.90:27017/metadata') and it no longer throws any error. But on the MongoDB side I don't see any new connections happening and my app does not start up on the machine. What should I be putting in the URI?
Your URI should indeed start with mongodb:
mongoose.connect('mongodb://username:password#host:port/database?options...');
See this page for more information: https://docs.mongodb.com/manual/reference/connection-string/
Did you try to connect to the database from cli? Or Telnet to see that connection isn't blocked.

Cannot connect to any rabbitmq cloud service

I was working fine with cloudamqp until all of a sudden wascally/rabbot stopped being able to connect to my endpoint. I have installed RabbitMQ locally and my system works fine. I have since then tried to setup a RabbitMq instance on Heroku via bigwig, to no avail. The endpoints I'm using should be fine and I also installed amqp.node and node-amqp to test if maybe it was a problem with rabbot. However none of these can connect either.
Any idea what the problem can be?
the most common cause is connection timeout. with all my wascally code, hosting on cloudamqp (with heroku, digital ocean or otherwise), i have to set a connection timeout much higher than the default for it to work.
this can be done with the connection_timeout parameter on the connection string url (https://www.rabbitmq.com/uri-query-parameters.html)
for example:
var conn = "amqp://myuser:mypassword#server.cloudamqp.com/my-vhost?connection_timeout=30"
this will set a connection timeout of 30 seconds

Using MongoLab in Heroku doesnt work unless I provide CC number?

I have developed a nodeJS application using Angular as the front-end (used MEAN stack and plan to use Bootstrap with it) and when I try to deploy it to Heroku, whenever I run this line: heroku addons:create mongolab I get the following output:
bruno#bruno-HP-epicsauce:~/herokuFinalApp$ heroku addons:create mongolab
! Please verify your account to install this add-on plan (please enter a credit card) For more information, see https://devcenter.heroku.com/categories/billing Verify now at https://heroku.com/verify
bruno#bruno-HP-epicsauce:~/herokuFinalApp$
And in both sites (mongolab and Heroku) it says it's free... I have already defined the heroku env variable called MONGOLAB_URI, like this:
bruno#bruno-HP-epicsauce:~/herokuFinalApp$ heroku config:set MONGOLAB_URI=mongodb://USER:PASS#ds041643.mongolab.com:41643/dbNAME
Setting config vars and restarting rocky-sea-9859... done, v4
MONGOLAB_URI: mongodb://USER:PASS#ds041643.mongolab.com:41643/dbNAME
bruno#bruno-HP-epicsauce:~/herokuFinalApp$ heroku config | grep MONGOLAB_URIMONGOLAB_URI: mongodb://USER:PASS#ds041643.mongolab.com:41643/dbNAME
bruno#bruno-HP-epicsauce:~/herokuFinalApp$
It still doesnt work...
In the git repository I have the following file structure:
The app is listening on the port: process.env.PORT instead of some local one and the package.json file contains the correct dependencies as it was automatically generated with npm init.
Inside the procfile I have the usual reference to the file that contains my server side code:
web: node server.js
The app.js contains the Angular client-side code.
Is there any 100% free way of deploying an app in Heroku with a DB hosted somewhere?
You have two options to use MongoLab services on Heroku:
Connect to existing MongoLab deployments from Heroku (no credit card required)
Add MongoLab as a Heroku add-on (credit card required)
Since you have set up MONGOLAB_URI, I assume you created a separate account with MongoLab and created a new MongoDB deployment. In this case you don't need an addon. If you have your MongoDB driver configured to use the connection URI, setting MONGOLAB_URI config on Heroku is usually enough.
More info on this here
Heroku asks for a credit card for verification purposes. It won't charge you unless you choose paid addons.
You must verify your account if you want to:
Add any add-on to your app, even if the add-on is free. The only exceptions to this are the free plans for the Heroku Postgres and Heroku Connect add-ons, which can be added without verification (source)
A message from their staff:
You won't be charged if you only use the free plan. Unfortunately some
people use free add-on plans for evil purposes so we need a way to
verify the account. Right now we do that by requesting a valid credit
card, and we may add more verification mechanisms in the future.
I'm in the same point than you, but as you can read, there is no problem to add that information (but to be honest I'd prefer another way as well).
I don't know it will actually charge you, it just wants CC to verify. But you could always just use http://progrium.viewdocs.io/dokku/ or Digital Ocean VPS (which is not actually harder than setting up your dev machine as long as you aren't expecting perfect security etc.).
You do not need mLab MongoDB add-on, just manually add Config Vars

keep separate the local database for local dev and nodejutsu database URL for nodejitsu app?

When I'm developing on my local box I use mongodb://localhost/Data as my connection string when connecting to mongoDB that's running locally.
But then when I upload/update my local app to nodejitsu I have to change the url to the one provided by nodejitsu mongodb://nodejitsu_XXX:xyxyxyxy.mongolab.com:29950/nodejitsu_XXX_nodejitsudb12121212
How do I switch between my localhost mongoDB connection URL and the one provided by Nodejitsu? I don't wanna keep changing it and committing the changes everytime I want to test out my local changes in the cloud...
I use nconf https://github.com/flatiron/nconf , you can easily change between diferent configurations

Resources