I intend to program a Bitcoin related Web application with Ruby on Rails. Now there are some very useful libraries out there like Bitcoinjs or Bitcore that are written in Javascript and can be run on the client side in the Webbrowser, or server side with Node.js. However, since I intend to program this application with RoR, I wonder how I can take advantage of those libraries on the server side. Is there a way (i.e. a gem) that allows interpreting Javascript on the server or can I start a Node.js instance in my RoR code by which I can then interpret those javascript libraries and execute their functions? Any help is greatly appreciated.
-
I wouldn't recommend it, try and look for native libraries instead of trying to run one in an environment it isn't made for. Doing so usually causes bad performance.recoup8063– recoup80632014-08-03 23:34:49 +00:00Commented Aug 3, 2014 at 23:34
-
Ok, but apart from the performance issue, is it possible and how would it be done?Reto– Reto2014-08-04 07:03:04 +00:00Commented Aug 4, 2014 at 7:03
1 Answer
Since it seams like you are very determined to use these nodejs libraries, here is how I would go about doing it. Since it is a very bad idea to run code in the wrong environment, I would instead make a setup like this.
Front end, ERB template => Rails server => nodeJs server
The idea behind this setup is that Rails is more like a task master and nodeJs is the worker. Your Rails app would figure out what needs to happen and then tell nodeJs to do it. Nodejs would then respond with the result which Rails would present back to the user.
Here is an example:
Someone on the front end asks you app to transfer x bitcoins to someone else. Your Ruby on Rails app would then do whatever non bitcoin tasks were needed(ex: logging in, messaging people). Then your Rails app would send a request to the nodeJs server which would actually do the bitcoin tasks.
Here is how you would go about implimenting this setup.
- Make the front end
- Connect frontend actions with your Rails controllers
- Make a RESTful HTTP API on your nodeJs.
- If you don't already know what a RESTful API is, research it. It is a whole separate topic
- Make an entry point for each action you want your app to perform.
- Make this nodeJs server run localy, so people can not access it without going through your Rails app
- Then use something like restful-client to use you nodeJs API from your Rails app