Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Meteor forever? And is it good using 3rd party database? [closed]

So, when I run meteor, app works fine. However, when I close the connection to my cloud server, meteor obviously shuts down. What should I do to run meteor forever?

Also, what's the point of using 3rd party database service like https://mongolab.com/? Doesn't it slow down the website, because now application has to connect to their database instead of local database?

And how exactly do I connect to mongolab for example?

like image 376
good_evening Avatar asked Sep 05 '14 02:09

good_evening


1 Answers

So, when I run meteor, app works fine. However, when I close the connection to my cloud server, meteor obviously shuts down.

You could launch meteor with nohup (no hang-up) which serves this purpose.

nohup meteor --production &

But it's not a good idea to run a site in production with meteor anyway.

What should I do to run meteor forever ?

You can use forever, a Node.js tool designed to run node apps as services.

I also want to point that forever is getting old and I've heard of better recent alternatives but it seems to still be a pretty common tool. You could also use systemd which integrates better with the UNIX service ecosystem but that's anoter story.

But first, you'll have to "demeteorize" your meteor application like this :

cd my-project-meteor
meteor bundle --directory ../my-project-node
# this is going to take some time
cd ../my-project-node/programs/server
npm install
# this is going to take some time too

So now you have a plain node app, that you can run with node main.js

Let me mention that it might be a good idea to use the node version used by meteor which is 0.10.29 as of meteor 0.9.1 You can install it traditionally or you could use the node version that is shipped with the meteor tool.

sudo ln -s ~/.meteor/packages/meteor-tool/1.0.27/meteor-tool-os.linux.x86_64/dev_bundle/bin/node /usr/bin/node
sudo ln -s ~/.meteor/packages/meteor-tool/1.0.27/meteor-tool-os.linux.x86_64/dev_bundle/bin/npm /usr/bin/npm

Note that this way of "installing" node + npm on your system is problematic because :

  • it assumes you're doing only meteor related stuff.
  • it is dependant on the release process of the meteor tool (you'll need to rerun these commends if the meteor tool is updated).

You can install the forever tool using npm :

# -g means globally : give access to forever to all users on the system
sudo npm install -g forever

To launch your node app as a service, you can use the following command, which sets correctly some environment variables and run the app using forever :

sudo PORT=80 MONGO_URL=mongodb://localhost/my-project-mongodb ROOT_URL=http://localhost forever start my-project-node/main.js

You can monitor it using forever stop my-project-node/main.js

Also, what's the point of using 3rd party database service like https://mongolab.com/?

When using the meteor tool, it launches a mongod process automatically for you, and the underlying node process executed by meteor representing your app connects to this mongo instance.

When we want to launch our meteor app as a node app, we have to handle the mongo stuff ourself, which kinda answer the question : why not using another service to handle it for us, they know better, right ?

Doesn't it slow down the website, because now application has to connect to their database instead of local database ?

Of course, relying on a 3rd party database service has its inconvenients, and this is one of them. Network communications will always be slower than interprocess communications taking place on localhost (this is especially true on these SSD backed cheap VPS you can find nowadays).

And how exactly do I connect to mongolab for example ?

By setting an appropriate value to the environment variable MONGO_URL, the database service provider will give you an url that corresponds to your online mongodb, this is what you need to pass to the node process in command line if you want meteor to connect to your distant database and work as usual.

If you want to launch a dedicated local mongod instance to let your application connect to it, well this is another topic but you'll have to follow these steps :

  • first install mongodb correctly on your server, using the reference documentation for the OS version. By correctly I mean choose the same version as meteor is using currently (2.4.9) and let it run as a service so that it will actually restart when your server reboots.
  • test that mongod is running by launching a client with the mongo command.
  • pass the correct MONGO_URL when launching your app with forever (something like mongodb://localhost/my-project-mongodb)

Understand now why meteor deploy is amazing :D

like image 175
saimeunt Avatar answered Nov 03 '22 03:11

saimeunt